You could use the -o
switch 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 -p
switch to select a specific PID:
$ ps -p [PID] -o args
pidof
may also be used to switch from process name to PID, hence allowing the use of -p
with a name:
$ ps -p $(pidof dhcpcd) -o args
Of course, you may also use grep
for this (in which case, you must add the -e
switch):
$ 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 grep
):
$ 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 $(...)
as usual:
$ CMDLINE=$(ps -p $(pidof dhcpcd) -o args --no-headers)
or, with grep
:
$ CMDLINE=$(ps -eo args | grep dhcpcd | head -n -1)
ps
(not recommended) or are you looking for some alternative command tops
that will give you the output? What needs to be done whenps
gives multiple lines of output? print all/first/last?arg
likecommand
as per @John answer?pidof <process name>
ps -o pid,args|grep $PID|tr -s " "|cut -d " " -f 2 But this way I don't get the args in one variable