alexkras.com

  • Home
  • Top Posts
  • Resume
  • Projects
  • YouTube
  • Boots to Bytes
  • About
  • Contact

How to Restart Python Script after Exception and Run it Forever

February 23, 2018 by Alex Kras 16 Comments

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.

Filed Under: Python

I work for Evernote and we are hiring!

Subscribe to this Blog via Email

New posts only. No other messages will be sent.

You can find me on LinkedIn, GitHub, Twitter or Facebook.

This blog is moving to techtldr.com

Comments

    Leave a Reply Cancel reply

  1. nicholas woodforth says

    January 18, 2020 at 4:23 pm

    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

    Reply
  2. logicalindian says

    November 14, 2019 at 9:21 pm

    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.

    Reply
    • Alex Kras says

      November 14, 2019 at 9:49 pm

      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.

      Reply
  3. cartman2490 says

    November 1, 2019 at 10:39 am

    Any chance I can get a windows batch file equivalent?

    Reply
  4. Courier Natix says

    April 11, 2019 at 5:14 pm

    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!

    Reply
  5. kostas says

    March 27, 2019 at 3:33 am

    great

    Thanks a lot

    Reply
  6. fakename. says

    January 7, 2019 at 9:03 pm

    This is very helpful. Thank you.

    Reply
  7. Erdener says

    December 27, 2018 at 2:47 pm

    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 "'
    ./forever.py: line 8:
    print(“\nStarting ” + filename)’

    What can be the problem?

    Reply
    • Alex Kras says

      January 8, 2019 at 10:26 am

      Not sure, do any other python scripts work?

      Reply
    • TK says

      February 14, 2020 at 5:27 pm

      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.

      Reply
  8. Anthony Nardelli says

    December 7, 2018 at 11:56 am

    Hello, I have an implementation where a python script calls two other scripts, is there a way to adapt your implementation with my example?

    Reply
  9. HITESH SALAVI says

    August 23, 2018 at 5:02 am

    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?

    Reply
    • Alex Kras says

      August 23, 2018 at 10:18 am

      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.

      Reply
  10. Сергей Мищенко says

    August 12, 2018 at 5:17 am

    Thank You VERY MUCH! <3

    Reply
  11. Ehsan Derakhshan says

    July 23, 2018 at 12:55 am

    This script help me a lot my friend thank you so much.

    Reply
  12. 10basetom says

    June 25, 2018 at 12:21 am

    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!

    Reply

Copyright © 2021 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in