Restart python-script from within itself

Restart python-script from within itself

Youre looking for os.exec*() family of commands.

To restart your current program with exact the same command line arguments as it was originally run, you could use the following:

os.execv(sys.argv[0], sys.argv)

I think this is a more elaborate answer, as sometimes you may end up with too many open file objects and descriptors, that can cause memory issues or concurrent connections to a network device.

import os
import sys
import psutil
import logging

def restart_program():
    Restarts the current program, with file objects and descriptors

        p = psutil.Process(os.getpid())
        for handler in p.get_open_files() + p.connections():
    except Exception, e:

    python = sys.executable
    os.execl(python, python, *sys.argv)

Restart python-script from within itself

UPDATE – of the above answer with some example for future reference

I have

kill -9
python /home/sun/workspace/c/src/ &

And i have where i need to restart the application itself, so i had:


but that did not restart the application itself by following, so when i used this way:

os.execl(, )

Then i was able to restart itself

Leave a Reply

Your email address will not be published. Required fields are marked *