1

I've edited my rc.local to start my python script when the Pi boots up (like here).

How can it be done to restart the script automatically if it crashes? It would be best if a re-start would be logged somewhere.

5
  • 1
    Surely your time would be better spent in correcting the errors which cause the script to crash.
    – joan
    Commented Oct 4, 2014 at 8:11
  • You are absolutely correct. But my humble experience tells me that no code is 100 % bug free. The script should be restarted no matter what happened.
    – Norbert
    Commented Oct 5, 2014 at 6:17
  • 1
    A simple software watchdog is probably best. Have the Python script you are monitoring touch a known temporary file every N seconds. Have a simple process check for the files existence every 2*N seconds and then delete the file. If it doesn't exist re-launch the monitored script.
    – joan
    Commented Oct 5, 2014 at 8:05
  • Have you checked the Kiosk Mode before? If it is only one app that you want to run on RPi, check this video: youtube.com/watch?v=c1kVBCZ77U0
    – AdnanG
    Commented Oct 8, 2014 at 14:19
  • A better answer is the one provided by dividuum on this question: raspberrypi.stackexchange.com/questions/28199/…
    – jpw
    Commented Jan 31, 2016 at 8:19

2 Answers 2

0

See my response on this related question : I advise you on using supervisord

0

Write another simpler, less-prone-to-bugs python program that monitors if the original is running?

in a similar situation a long time back, I had my "main" python program "touch" (update timestamp) of a particular file after every main operation. Then, I had a second program which would keep monitoring the timestamp of the file. If the file wasn't updated in the last 20 minutes, it meant my original program had crashed.

Another instance, I wrapped my 'main' program around a script that went like this

#!/bin/bash
/path/to/my/code
email "Ok something went wrong, starting again in 5 minutes"

at now + 5 minutes -f /path/to/this/shell/script

That way, if the program crashed, it would send me an email and restart the program again.

2
  • very bad advice, because python scripts don't crash for no reason, and you'd better find the reason than hide it away.
    – lenik
    Commented Nov 7, 2014 at 10:21
  • The reason is not always in your control. When working with hardware, the hardware can crash due to a variety of reasons and python can do nothing about it . sure you can catch exceptions in python and what not but some times it is easier to work around it than be pedantic about your code perfectness. I am not disagreeing with you, just saying sometimes you have to do what is fast if doing the right thing might take too much time, especially in the corporate world. For pet projects, absolutely. Commented Nov 7, 2014 at 11:57

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