monitoring/restarting an application

Giampaolo Rodola' gnewsg at gmail.com
Fri Mar 6 09:02:49 EST 2009


On 5 Mar, 15:37, Ghirai <ghi... at ghirai.com> 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.
Example:

import psutil
for process in psutil.process_iter():
    if (process.pid > 1000) and (process.name ==
'your_executable_name'):
        process.kill()
        # restart your process here
        # ...


[1] http://code.google.com/p/psutil

--- Giampaolo
http://code.google.com/p/pyftpdlib



More information about the Python-list mailing list