How do you check if a program/process is running using python?

Venky K Shankar vshankar0 at gmail.com
Sat Jul 19 15:10:31 EDT 2008


On Sunday 20 July 2008 12:08:49 am Lamonte Harris wrote:
> How do you check if a program or process is running when using python? 
> What I want to do is have an infinite loop to check if a program is running
> or not and send data to my web server to check yes or no.  Is this
> possible? If so how?

you can execute OS system call.

here i execute ps -ef and grep the required process name (or you can grep by 
pid on that particular column using awk)

import os
os.system("ps -ef | grep -v grep | grep <proc_name>")  

in my system it gives result 0 if the process is running

>>> a = os.system("ps -ef | grep -v grep | grep python")
root      8749  5331  0 Jul19 pts/1    00:00:00 python
>>> a
0

else gives non 0 :

>>> a = os.system("ps -ef | grep -v grep | grep python123")
>>> a
256

you can check this inside while True:

I am sure there should be a far better solution that this in python.

	-Venky



More information about the Python-list mailing list