23

When I do ps -ef|grep python I get the following:

myusername  4492  2994  0 10:32 pts/0    00:00:01 /home/myusername/.virtualenvs/myproject/bin/ipython manage.py runserver
root        6665     1  0 10:42 ?        00:00:00 /usr/bin/python /usr/lib/system-service/system-service-d
myusername 14051 13497  0 11:28 pts/7    00:00:00 grep --color=auto python

How do I get only the user who is running the process, the pid and the command run for the process as in the following output instead?

myusername  4492 /home/myusername/.virtualenvs/myproject/bin/ipython manage.py runserver
root        6665 /usr/bin/python /usr/lib/system-service/system-service-d
3
  • Does this work? ps --fields="user pid command"
    – pratnala
    Mar 19, 2013 at 10:53
  • Didn't try it. Found it on the net. Not near a Ubuntu machine now. Sorry. Will see later and let you know
    – pratnala
    Mar 19, 2013 at 11:20
  • Nope, I get ERROR: Unknown gnu long option. Does it work for you?
    – Bentley4
    Mar 19, 2013 at 11:20

4 Answers 4

18

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
9
  • 4
    Does that actually work for you? When I run that command I get Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
    – Bentley4
    Mar 19, 2013 at 11:07
  • Yes it works for me in ksh under OpenSolaris. The output is as you desire... Check the man pages for ps on your system man ps. Maybe the syntax is slightly different on your system.
    – Simon
    Mar 19, 2013 at 11:17
  • 1
    But the OP is on bash and not on ksh
    – pratnala
    Mar 19, 2013 at 11:20
  • 1
    @Bentley4 If you just want the command and don't need additional information you can drop the -f argument and use the following command: ps -o uid,pid,cmd -e|grep python
    – Simon
    Mar 19, 2013 at 12:49
  • 1
    @Bentley4: ps -o user,pid,cmd -e|grep '[p]ython'
    – Simon
    Mar 19, 2013 at 13:45
4

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.

2
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 format
  • user,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
1

My version of PS is different, so it might require some tweaking, but you can use cut (and possibly tr depending on what you are trying to achieve) - for example something like

ps ef | cut -c1-16,50-   

Will provide the characters 1-16 and 50 onwards from each line of your ps statement. (Your actual numbers will probably need a bit of massaging).

Another way to do it (but you will loose formatting) might be

ps ef | tr -s " " | cut -f1,2,8- -d" "

Which will compress the whitespace in the ps command, then take fields 1,2 and 8 onwards and display them.

1
  • For only one process I get a half page of information on a full screen with those commands. It's pretty unreadable, I'm 'massaging' the parameters but so far I haven't been able to get anything remotely of what I want.
    – Bentley4
    Mar 19, 2013 at 11:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .