tags:

views:

51

answers:

3

I'm writing some monitoring scripts in Python and I'm trying to find the cleanest way to get the process ID of any random running program given the name of that program

something like

ps -ef | grep MyProgram

I could parse the output of that however I thought there might be a better way in python

A: 

From the standard library:

os.getpid()
Swingley
That gets the pid of the python process not what the user is asking for
Mark
+4  A: 

If you are not limiting yourself to the standard library, I like psutil for this.

Mark
A: 

Try pgrep. Its output format is much simpler and therefore easier to parse.

lunaryorn
exactly what I needed, thanks