Python process automatically restarting itself

Adam Atlas adam at atlas.st
Thu Oct 11 10:17:12 EDT 2007


What is the best way for a Python process (presumed to be a script run
by the interpreter binary, not embedded in some other program) to
restart itself? This is what I've been trying:

    import __main__

    for path in sys.path:
        path += '/' + __main__.__file__
        if os.access(path, os.F_OK): break
    else:
        raise Exception('WTF?')

    #Then, something like...
    os.execl(sys.executable, sys.executable, path, *sys.argv[1:])

BUT! This is a multi-threaded program, and at least on OS X, trying to
exec*() in a threaded process raises OSError [Errno 45] Operation not
supported. So I tried having it fork, and having the child process
wait for the parent process to die (in various ways) and then exec*().
That seemed to work, sort of, but the child would not be attached to
the original terminal, which is a must.

Any ideas?




More information about the Python-list mailing list