James James - 1 year ago 76
Python Question

How do you get the process ID of a program in Unix or Linux using Python?

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download