Restarting scripts

Brian Cole brianc at temple.edu
Mon Jan 16 19:51:31 EST 2006


If exec can't find python, use sys.executable. If exec can't find the
program, use os.path.abspath(__file__).

The benefit of "exec" is that the process will always use the same
PID. If that's not a requirement using a loader program would be a lot
easier.

Instead of os.spawn, if you have python 2.4, you can use the
subprocess module. It's alot more robust and "Pythonic".

from subprocess import Popen
while True:
    child=Popen(["prog","arg"])
    child.wait()

The issue then becomes how to properly shut down the server in the
event of needing to reboot the machine. There's multiple ways to do
this, some are:
1. Check the return status of child.wait(), if child exitted normally
"0", die quietly, else restart the server
2. Use signal handling in the loader program to take down the server
when the sent SIGINT. Check out the signal module.

There's probably other inventive ways to do this. It mostly depends on
how your program is written.

-Brian Cole

On 1/16/06, Ron Griswold <RGriswold at rioting.com> wrote:
> Thank you Brian. I gave this a shot and found that execlp was not able
> to locate either python, or my script... not sure which.
>
> Someone on another list has suggested using a loader program that uses
> os.spawnl to start the child process, and if it exits at any point you
> simply have that call to spawnl in an infinite loop and the server will
> start up again immediately. Unfortunatly I haven't got that to work
> either. Though I'm calling it with the P_WAIT flag, it's returning
> immediately as though I had used P_NOWAIT.
>
> Thanks for the help!
>
> Ron Griswold
> Character TD
> R!OT Pictures
> rgriswold at rioting.com
>
>
> -----Original Message-----
> From: coleb2 at gmail.com [mailto:coleb2 at gmail.com] On Behalf Of Brian Cole
> Sent: Monday, January 16, 2006 1:46 PM
> To: Ron Griswold
> Cc: python-list at python.org
> Subject: Re: Restarting scripts
>
> 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
> script.
>
> -Brian
>
> On 1/16/06, Ron Griswold <RGriswold at rioting.com> wrote:
> >
> >
> >
> > This may be a little OT, but it does involve python scripts. I've
> written a
> > server that hosts my companies asset management system. Of course
> something
> > like this cannot be down for very long without data being lost (of
> course I
> > have taken measures to inform the user of the down time and ask them
> nicely
> > to please try again later... but in the real world people will just
> bypass the
> > system and save manually). So what I need is some facility to start
> the
> > server, and if it goes down, automatically start it back up again.
> >
> >
> >
> > If anyone has any ideas on how I might accomplish this, either with
> python
> > code, or third party software, I'd love to hear about it.
> >
> >
> >
> > Thanks,
> >
> >
> >
> > Ron Griswold
> >
> > Character TD
> >
> > R!OT Pictures
> >
> > rgriswold at rioting.com
> >
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
>



More information about the Python-list mailing list