I have some processes that I need to keep running on a linux host. For some reason the processes are dying (possibly being killed by another admin). I can hunt that down later, right now I need to make sure the processes are running and relaunch them if necessary

Launching the processes is no problem

import subprocess

proc1 = subprocess.Popen('first command')
proc2 = subprocess.Popen('second command')

But how do I monitor the processes and make sure that they are still running? I tried proc1.poll() but it always returns zero whether the program is running or not. I figure I would just set up a while loop that would go through each process once I know what to do with them.

Any ideas?

Recommended Answers

All 2 Replies

I don't have much experience with this, so only have used ps to a file and parse the file when this is necessary. This may require some tweaking.

def ps_test():
    fname = "./example_ps.txt"
    os.system("ps -AFL > " + fname)     ## or subprocess
    data_recs = open(fname, "r").readlines()
    for rec in data_recs:
         if the_program_name in rec :
             print "Found", rec
             return 1     ## program is running
    return 0      ## Not found

Edit: multiprocessing/pyprocessing has a getPID()and isalive() function that you may be able to use. http://www.doughellmann.com/PyMOTW/multiprocessing/basics.html#daemon-processes

The thing you're trying to accomplish is called a watchdog. Search google for "process watchdog" "watchdog timer" or similar queries and they should be of some help to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.