Here is a simple trick that I used to restart my python script after unhandled exception.
Let’s say I have this simple script called test.py that I want to run forever. It will just wait 2 seconds and throw an error.
import time time.sleep(2) raise Exception("Oh oh, this script just died")
I use the following script called forever in the same directory:
#!/usr/bin/python from subprocess import Popen import sys filename = sys.argv[1] while True: print("\nStarting " + filename) p = Popen("python " + filename, shell=True) p.wait()
It uses python to open test.py
as a new subprocess. It does so in an infinite while loop, and whenever test.py
fails, the while loop restarts test.py
as a new subprocess.
I’ll have to make the forever script executable by running chmod +x forever
. Optionally forever
script can be moved to some location in the PATH
variable, to make it available from anywhere.
Next, I can start my program with:
./forever test.py
Which will result in the following output:
Starting test.py Traceback (most recent call last): File "test.py", line 4, in <module> raise Exception("Oh oh, this script just died") Exception: Oh oh, this script just died Starting test.py Traceback (most recent call last): File "test.py", line 4, in <module> raise Exception("Oh oh, this script just died") Exception: Oh oh, this script just died Starting test.py
As you can tell, this script will run repeatedly, until it is killed with ctr+c
.
this works great 🙂
one issue i have though is that i need to do it with sudo. is there a way to do this where it enters the password aswell?
everything i have found dont look to work right
Excellent Thanks.. Exactly what i am looking for… anyways can you pls tell me how to stop this based on time.. for eg once the time is 17:00:00, this forever script should stop.
Of the top of my head, if you google how to get current date in Python, you should be able to extract hour of the day, then you can add logic to your script to exit, when that hour is hit.
Any chance I can get a windows batch file equivalent?
I’m trying this out now, I have two discord bots and recently they’ve been crashing at random without saying why, even with logging. I’ve been looking for a solution to my problem for weeks now and so far, your solution is working great! Thank you for sharing this!
great
Thanks a lot
This is very helpful. Thank you.
Alex thanks for your guidance.
It worked in Debian GNU/linux machine but I’m getting error on Linux 18.04. The error message:
./forever.py: /usr/bin/python: bad interpreter: No such file or directory
When I removed first line the error was being like that:
./forever.py: line 2: from: command not found
./forever.py: line 3: import: command not found
./forever.py: line 4: import: command not found
./forever.py: line 6: filename: command not found
./forever.py: line 8: syntax error near unexpected token
"\nStarting "'
print(“\nStarting ” + filename)’./forever.py: line 8:
What can be the problem?
Not sure, do any other python scripts work?
You’re trying to invoke Python 2’s interpreter, instead of Python 3. Your very first line should look like
#!/usr/bin/python3
.print('some text')
– Python 3 (print function)print 'some text'
– Python 2 (print statement)I might have been too late for you, but maybe it will help someone else in the near, but distant, space-time.
Hello, I have an implementation where a python script calls two other scripts, is there a way to adapt your implementation with my example?
I have a flask framework implementation. I have a global variable which is being used in a route(ie. /xyz) and nowhere else.
When I don’t run /xyz, the global variable gets vanished(my may or may not accurate #AfterDebuggingConclusion). How can I keep the variable in the memory if my conclusion is right?
Hmm, I think if you declare it outside of scope of this /xyz method it should exist even if method did not run. Then you can reference it inside of function by declaring it
global xyz
.Thank You VERY MUCH! <3
This script help me a lot my friend thank you so much.
Thanks for this great tip. I’m trying to output the subprocess script’s
print
statements to the screen, but so far no success :(. Here’s what I have so far:import subprocess
import sys
filename = ‘my_script.py’
while True:
print(‘Starting ‘ + filename + ‘ …’)
p = subprocess.Popen(‘python ‘ + filename, shell=True, stdout=subprocess.PIPE)
(output, err) = p.communicate()
print(output)
p.wait()
It would be awesome if you could point me in the right direction. Thanks!