8

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

codeforester
  • 39,467
  • 16
  • 112
  • 140
Rohin Kumar
  • 730
  • 2
  • 10
  • 24
  • `execl` and `execv` are operating system commands, you can read their documentation by typing `man execl` (or go [here](http://linux.die.net/man/3/execl)). – DanielGibbs Jul 16 '15 at 07:08

2 Answers2

11

At the low level they do the same thing: they replace the running process image with a new process.

The only difference between execv and execl is the way they take arguments. execv expects a single list of arguments (the first of which should be the name of the executable), while execl expects a variable list of arguments.

Thus, in essence, execv(file, args) is exactly equivalent to execl(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 to exec* should be

['python', __file__] + sys.argv[1:]

I have just tested a restart script with the following:

os.execl(sys.executable, 'python', __file__, *sys.argv[1:])

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".

William Miller
  • 9,839
  • 3
  • 25
  • 46
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Interesting! does it have to do with the way it restarts the script also? – Rohin Kumar Jul 16 '15 at 07:14
  • The difference between `execl` and `execv` will not affect the restarting of the script at all, unless you pass different arguments to them. – nneonneo Jul 16 '15 at 07:20
  • Hmm... I can actually see the program restart in front of my eyes (have UI closing and starting again). So I doubt "continuing where I left off" argument – Rohin Kumar Jul 16 '15 at 07:23
  • OK, so what the heck do you mean by "start from where the previous script left"? If you can clearly see the program restart, but it still starts from the previous state, then you have a bug in your code (or your code is restoring its state somehow). – nneonneo Jul 16 '15 at 07:27
  • I am a hardware guy trying to code a full-fledged python script for the first time... so be gentle if I say something wrong... what I meant was, when the code restarts, it is stuck in the same loop instead of coming from line 1 let's say. (I also have a thread running.) May be you can look at the code here (http://stackoverflow.com/questions/31358737/monitoring-if-a-file-stopped-writing-in-python) – Rohin Kumar Jul 16 '15 at 07:32
  • I was also referring to this line "The current process is replaced immediately" in here https://docs.python.org/2/library/os.html#os.execlp – Rohin Kumar Jul 16 '15 at 07:36
  • Now it's pretty clear you have [an XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You don't actually want to restart the program - you want to wait a bit, and then retry the operation. Might I suggest a `while` loop with a `time.sleep()`? – nneonneo Jul 16 '15 at 08:07
  • Thanks for helping me in better participation of the forums... Will see if sleep(n) actually helps – Rohin Kumar Jul 16 '15 at 09:15
  • Now come to think of it, you got me wrong! I WANT to restart the program... sleep is useless in my case as it will hang the UI. not an XY problem... I gave the background as a reference to what was happening in my program! – Rohin Kumar Jul 17 '15 at 12:04
0

According to the Python documentation there's no real functional difference between execv and execl:

The “l” and “v” variants of the exec* functions differ in how command-line arguments are passed. The “l” variants are perhaps the easiest to work with if the number of parameters is fixed when the code is written; the individual parameters simply become additional parameters to the execl*() functions. The “v” variants are good when the number of parameters is variable, with the arguments being passed in a list or tuple as the args parameter. In either case, the arguments to the child process should start with the name of the command being run, but this is not enforced.

No idea why one seems to restart the script where it left off but I'd guess that that is unrelated.

DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • I got the idea from http://blog.petrzemek.net/2014/03/23/restarting-a-python-script-within-itself/. Look at the Restarting the Script section – Rohin Kumar Jul 16 '15 at 07:14
  • Yes, that section says that `execv` "... starts executing the current script from its beginning." There's nothing to suggest that `execl` will do otherwise. – DanielGibbs Jul 16 '15 at 07:15
  • well... my script gets stuck in the same loop even after restarting when I am using `execl`. Not sure about `execv` coz it is driven by some random hardware issue – Rohin Kumar Jul 16 '15 at 07:20
  • Well that's a separate issue with your script but shouldn't be anything to do with your use of `execv`/`execl`. – DanielGibbs Jul 16 '15 at 07:24
  • ! if that's true, `execv` should also get stuck in the same loop. I will report when it occurs... since it is random, I don't know when... Thanks for your inputs anyways! – Rohin Kumar Jul 16 '15 at 07:28