how can I find out the process ids with a process name

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Sep 2 18:53:32 EDT 2007


On Sun, 02 Sep 2007 19:26:27 +0000, herman wrote:

> But now I would like to do the programmically in my python script. I
> know I can use ' os.system(cmd)' to execute the command 'ps -ef | grep
> emacs', but how
> can I pipe the output of my 'ps -ef | grep emacs' to my python script
> and then run a regression expression with it to get the process Ids?

Use popen.

>>> f = os.popen('ps ax | grep -i PYTHON')
>>> print f.read()
 1952 ?        Ssl    0:01 /usr/bin/python -E /usr/sbin/setroubleshootd
 2117 ?        S      0:00 python ./hpssd.py
 2376 ?        SN     3:19 /usr/bin/python /usr/sbin/yum-updatesd
18087 pts/4    S+     0:00 python
18115 pts/4    S+     0:00 sh -c ps ax | grep -i PYTHON
18117 pts/4    R+     0:00 grep -i python


There is also a module popen2 which does similar but more advanced things.



-- 
Steven.



More information about the Python-list mailing list