How to get only the user, the pid and the command run for a specific process? (Ubuntu 11.10)
I guess you are looking for the -o argument:
-o format:
user-defined format. format is a single argument in the form of a blank-separated or comma-separated list, which offers a way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be output. Column width will increase as needed for wide headers; this may be used to widen up columns such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control (ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality; output may be one column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options when in doubt. Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and DefBSD are macros that may be used to choose the default UNIX or BSD columns.
So the command you want would be (Ubuntu):
ps -o uid,pid,cmd -ef|grep python
under OpenSolaris the command is:
ps -o ruser,pid,comm -ef|grep python
The simplest would probably be:
$ ps o uid=,pid=,cmd= -C python
1000 26126 python
That way you get everything directly from ps
and don't need to parse anything.
From the ps
man page:
-o format
User-defined format. format is a single argument in the form of a blank-separated or comma-separated list, which offers a way to specify individual output columns. [...] Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be output.
-C cmdlist
Select by command name. This selects the processes whose executable
name is given in cmdlist.
The -C
option will work if you are running python
interactively, not if python is running a script. In that case you should use -C scriptname.py
instead.
ps -eo user,pid,cmd | grep [p]ython
Example:
$ ps -eo user,pid,cmd | grep [p]ython
root 1056 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root 1735 /usr/bin/python3 /usr/bin/fail2ban-server -xf start
user 16613 /usr/bin/python3 /usr/share/system-config-printer/applet.py
Explanation:
-e
all processes-o
user-defined formatuser,pid,cmd
Show user, process ID, command columns
Note: if you use -f
with -o
as others have suggested, you may get errors. This is because both of these parameters control the output format, and only one of them should be used:
$ ps --help | grep -A 2 "output format"
*********** output format **********
-o,o user-defined -f full
-j,j job control s signal