Is there a difference between os.execl() and os.execv() in python? I was using
os.execl(python, python, *sys.argv)
to restart my script (from here). But it seems to start from where the previous script left.
I want the script to start from the beginning when it restarts. Will this
os.execv(__file__,sys.argv)
do the job? command and idea from here. I couldn't find difference between them from the python help/documentation. Is there a way do clean restart?
For a little more background on what I am trying to do please see my other question
At the low level they do the same thing: they replace the running process image with a new process.
The only difference between
execv
andexecl
is the way they take arguments.execv
expects a single list of arguments (the first of which should be the name of the executable), whileexecl
expects a variable list of arguments.Thus, in essence,
execv(file, *args)
is exactly equivalent toexecl(file, args)
.Note that
sys.argv[0]
is already the script name. However, this is the script name as passed into Python, and may not be the actual script name that the program is running under. To be correct and safe, your argument list passed toexec*
should beI have just tested a restart script with the following:
and this works fine. Be sure you're not ignoring or silently catching any errors from
execl
- if it fails to execute, you'll end up "continuing where you left off".According to the Python documentation there's no real functional difference between
execv
andexecl
:No idea why one seems to restart the script where it left off but I'd guess that that is unrelated.