PDA

View Full Version : Python - How to check for a running process?



dodle
March 20th, 2009, 10:23 PM
I'm trying to figure out how to check and see if a program is already running, to keep from executing the program a second time.

The program that I am trying to check is x11vnc, which is run from the command line.

days_of_ruin
March 20th, 2009, 10:42 PM
I'm trying to figure out how to check and see if a program is already running, to keep from executing the program a second time.

The program that I am trying to check is x11vnc, which is run from the command line.

Maybe there are better ways but this works for me:

import commands
output = commands.getoutput('ps -A')
if 'x11vnc' in output:
print "ITS ALIVE!!!"

dodle
March 20th, 2009, 11:03 PM
Thanks, so far that works really well.

ghostdog74
March 21st, 2009, 02:33 AM
if Python is not a must, just do it from the shell


# ps -A | awk '/x1nvc/{print "alive";exit}'
alive