monitoring/restarting an application

Giampaolo Rodola' gnewsg at
Fri Mar 6 15:02:49 CET 2009

On 5 Mar, 15:37, Ghirai <ghi... at> wrote:
> Hello list,
> I need to keep x number of instances of an external applications
> running, say /bin/x, but also kill and restart each one after y seconds.
> What would be the best way to do this (with python 2.5.x)?
> I'm thinking of having a list of running pids, then have a thread check
> if len(list) < x, and if it is, then start a new process (with
> os.spawnv?) and add it to the list.
> Also in the list i'd keep PIDs and some sort of started-timestamp, so i
> know which to kill.
> Does this sound reasonable, or is there an easier way?
> Thanks.
> --
> Regards,
> Ghirai.

Maybe you might want to take a look at psutil [1].
Rather than looking for PID->timestamp you can look for something else
like PID->cmdline or PID->executable_name.

import psutil
for process in psutil.process_iter():
    if ( > 1000) and ( ==
        # restart your process here
        # ...


--- Giampaolo

More information about the Python-list mailing list