[Baypiggies] Dumb "keep it running" script

Benjamin Sergeant bsergean at gmail.com
Wed Sep 23 21:02:24 CEST 2009


Hi there,

I figured out this morning that I had a long-running process that died
one week ago for some obscure reasons. I made a little script that
restarts a script when the called script die. I know there are already
existing stuff doing that but I could not remember the name / find any
existing ones  ... so why not re-inventing the wheel one more time :)

Thanks for any feedback,
- Benjamin

http://code.activestate.com/recipes/576911/
#!/usr/bin/env python

import sys
import time
import subprocess

"""
Keep a process up and running

If you have a long running process that can be killed for strange and unknown
reason, you might want it to be restarted ... this script does that.

$ cat alive.sh
#!/bin/sh

while `true`; do echo Alive && sleep 3 ; done

Use it like this:
$ keepup.py ./alive.sh
"""

cmd = ' '.join(sys.argv[1:])

def start_subprocess():
    return subprocess.Popen(cmd, shell=True)

p = start_subprocess()

while True:

    res = p.poll()
    if res is not None:
        print p.pid, 'was killed, restarting it'
        p = start_subprocess()

    time.sleep(1)


More information about the Baypiggies mailing list