How to get whole command line from a process?
How can I get the command arguments or the whole command line from a running process using its process name?
For example this process:
# ps PID USER TIME COMMAND 1452 root 0:00 /sbin/udhcpc -b -T 1 -A 12 -i eth0 -p /var/run/udhcpc.eth0.pid
And what I want is
/sbin/udhcpc -b -T 1 -A 12 -i eth0 -p /var/run/udhcpc.eth0.pidor the arguments. I know the process name and want its arguments. I'm using Busybox on SliTaz.
Do you want to process the output from `ps` (not recommended) or are you looking for some alternative command to `ps` that will give you the output? What needs to be done when `ps` gives multiple lines of output? print all/first/last?
I edited my question. I know the process name and want its arguments. I'm using SliTaz (minimalistic linux) with Busybox.
Yes but how to write it to a variable? I think of two command: PID=`pidof ` ps -o pid,args|grep $PID|tr -s " "|cut -d " " -f 2 But this way I don't get the args in one variable
@Michael I edited my answer accordingly. You should be more specific about your needs when writing your question ;)
You could use the
-oswitch to specify your output format:
$ ps -eo args
From the man page:
Command with all its arguments as a string. Modifications to the arguments may be shown. [...]
You may also use the
-pswitch to select a specific PID:
$ ps -p [PID] -o args
pidofmay also be used to switch from process name to PID, hence allowing the use of
-pwith a name:
$ ps -p $(pidof dhcpcd) -o args
Of course, you may also use
grepfor this (in which case, you must add the
$ ps -eo args | grep dhcpcd | head -n -1
GNU ps will also allow you to remove the headers (of course, this is unnecessary when using
$ ps -p $(pidof dhcpcd) -o args --no-headers
On other systems, you may pipe to AWK or sed:
$ ps -p $(pidof dhcpcd) -o args | awk 'NR > 1' $ ps -p $(pidof dhcpcd) -o args | sed 1d
Edit: if you want to catch this line into a variable, just use
$ CMDLINE=$(ps -p $(pidof dhcpcd) -o args --no-headers)
$ CMDLINE=$(ps -eo args | grep dhcpcd | head -n -1)
@Pandya Both `cmd` and `command` are aliases to `args`, this is probably unnecessary.
You may use `grep` to catch the line you're actually interested in: `ps -eo args | grep dhcpcd | head -n -1`. I edited my answer.
The command line appears to be truncated at 4096 bytes in the middle of an argvalue. According to https://unix.stackexchange.com/questions/91561/ps-full-command-is-too-long this is a problem with older kernels.