4

In a program I am writing in python I need to completely restart the program if a variable becomes true, looking for a while I found this command:

while True:
    if reboot == True:
        os.execv(sys.argv[0], sys.argv)

When executed it returns the error [Errno 8] Exec format error. I searched for further documentation on os.execv, but didn't find anything relevant, so my question is if anyone knows what I did wrong or knows a better way to restart a script (by restarting I mean completely re-running the script, as if it were been opened for the first time, so with all unassigned variables and no thread running).

2

3 Answers 3

7

There are multiple ways to achieve the same thing. Start by modifying the program to exit whenever the flag turns True. Then there are various options, each one with its advantages and disadvantages.

Wrap it using a bash script.

The script should handle exits and restart your program. A really basic version could be:

#!/bin/bash
while :
do
    python program.py
    sleep 1
done

Start the program as a sub-process of another program.

Start by wrapping your program's code to a function. Then your __main__ could look like this:

def program():
  ### Here is the code of your program
  ...

while True:
  from multiprocessing import Process
  process = Process(target=program)
  process.start()
  process.join()
  print("Restarting...")

This code is relatively basic, and it requires error handling to be implemented.

Use a process manager

There are a lot of tools available that can monitor the process, run multiple processes in parallel and automatically restart stopped processes. It's worth having a look at PM2 or similar.

IMHO the third option (process manager) looks like the safest approach. The other approaches will have edge cases and require implementation from your side to handle edge cases.

Sign up to request clarification or add additional context in comments.

Comments

1

This has worked for me. Please add the shebang at the top of your code and os.execv() as shown below

#!/usr/bin/env python3
import os
import sys

if __name__ == '__main__':
    while True:
        reboot = input('Enter:')
        if reboot == '1':
            sys.stdout.flush()
            os.execv(sys.executable, [sys.executable, __file__] + [sys.argv[0]])
        else:
            print('OLD')

Comments

1

I got the same "Exec Format Error", and I believe it is basically the same error you get when you simply type a python script name at the command prompt and expect it to execute. On linux it won't work because a path is required, and the execv method is basically encountering the same error.

You could add the pathname of your python compiler, and that error goes away, except that the name of your script then becomes a parameter and must be added to the argv list. To avoid that, make your script independently executable by adding "#!/usr/bin/python3" to the top of the script AND chmod 755.

This works for me:

#!/usr/bin/python3
# this script is called foo.py

import os
import sys
import time

if (len(sys.argv) >= 2):
    Arg1 = int(sys.argv[1])
else:
    sys.argv.append(None)
    Arg1 = 1

print(f"Arg1: {Arg1}")

sys.argv[1] = str(Arg1 + 1)
time.sleep(3)

os.execv("./foo.py", sys.argv) 

Output:

Arg1: 1
Arg1: 2
Arg1: 3
.
.
.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.