Restarting scripts

Ron Griswold RGriswold at
Mon Jan 16 20:51:03 EST 2006

I gave this a shot but haven't had much luck with it. When I try
os.execlp I get an error saying unknown file or directory. Whether this
is in reference to Python or my script I don't know, however python is
in my system's path variable and I'm running the script from the dir it
is located in.

At someone else's suggestion I tried using a loader script with
os.spawnv using the os.P_WAIT flag. Unfortunatly this exits right away
(presumably never starting my server). 

I also tried subprocess.Popen and then calling child.wait(). This sent
me into a loop of exceptions saying that the socket the server is
opening is already in use (even on the first try).

Does anyone know of a nice reliable app that already does this for you?
Open source preferably.

Ron Griswold
Character TD
R!OT Pictures
rgriswold at

-----Original Message-----
From: at
[ at] On Behalf
Of Peter Hansen
Sent: Monday, January 16, 2006 5:40 PM
To: python-list at
Subject: Re: Restarting scripts

Brian Cole wrote:
> If everything dies by a Python exception...
> if __name__ == '__main__':
>     try:
>         main(sys.argv)
>     except:
>         os.execlp("python", "python", sys.argv)
> Pretty nasty peice of code, but it works well for restarting your

Noting that sys.exit() and possibly several other things can "raise 
SystemExit" and while it is a subclass of Exception (and gets caught by 
the above), it is usually considered a clean and intentional exit and 
changing the above to do this is probably best:

except SystemExit:
    os.execlp(.... etc etc



More information about the Python-list mailing list