I have a Python script which ideally runs indefinitely in a loop. I am catching some exceptions, however, the problem is when some error occurs and the script stops because of that error. I want to detect somehow this situation and restart the script.

As I have some logic with time inside script, I don't see an option with another script running a sub-process script as a good one.

  • 1
    What error is occurring that stops your script? Why can't you catch it?
    – CoderMike
    Oct 26, 2019 at 11:55
  • 3
    You can do this putting your instructions in a while loop. while True: , and use exception KeyboardInterrupt for exiting while loop when you press [CTRL+C]. You can also use a system unit file for starting your python script, the file can contain : Restart, Timeout etc ... (I think it's the best method). You can see the options here
    – Ephemeral
    Oct 26, 2019 at 12:06
  • 1
    It communicates with google sheets api and usually error occurs there. As it is important that it has almost 99 percent running time I would like to ensure that it restarts Oct 26, 2019 at 12:09
  • 1
    @PostarLakogSna, google not provide any exception handler for your error ? Have you try to put a global try: Except for getting your error description ?
    – Ephemeral
    Oct 26, 2019 at 12:28
  • 2
    Correction: (I think it's the best method) ... no the best method is to check the errors correctly first and then maybe use a system unit file.
    – Ephemeral
    Oct 26, 2019 at 12:30

3 Answers 3


You can run the script as service using systemd. Just create a Unit file with:

rpi ~$ sudo systemctl --force --full edit myscript.service

In the empty editor insert these statements, save them and quit the editor:

Description=My Python Script

ExecStart=python3 /full/path/to/myscript.py


This will always restart your script after 10 seconds. Always restarting a script may not be the best option if it should terminate regular. You can use the settings no, on-success, on-failure, on-abnormal, on-watchdog, on-abort or always. There are many other options to restart the script as you can find in man systemd.service.

Enable and run the service with

rpi ~$ sudo systemctl enable --now myscript.service

Check it with

rpi ~$ systemctl status myscript.service

Consider using the continue statement to restart the while loop if you can detect the error with an "if" statement:

while True:
    <all your code>

    if <some error occurs>:

The continue statement will bring control back to the top of your while loop

  • 1
    You don't need that continue instruction at all as long as try is in the right scope (which is the highest scope in the while loop, like you correctly did)
    – Sim Son
    Oct 26, 2019 at 20:14
  • You should throw a delay in there of at least a few seconds so a problem with the code failing immediately does not lead to busy looping.
    – goldilocks
    Oct 27, 2019 at 13:21

For error handling you can use Try-Except https://www.w3schools.com/python/python_try_except.asp . That way you decide what to do with every error or exception (so you basically MUST decide every time).

Your code would have to be very well structured (even with lots of try-excepts) in order to "catch them all", but that's something worth working on.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.