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.
-
1Surely your time would be better spent in correcting the errors which cause the script to crash.joan– joan2014年10月04日 08:11:09 +00:00Commented 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– Norbert2014年10月05日 06:17:27 +00:00Commented Oct 5, 2014 at 6:17
-
1A 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– joan2014年10月05日 08:05:04 +00:00Commented 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=c1kVBCZ77U0AdnanG– AdnanG2014年10月08日 14:19:42 +00:00Commented Oct 8, 2014 at 14:19
-
A better answer is the one provided by dividuum on this question: raspberrypi.stackexchange.com/questions/28199/…jpw– jpw2016年01月31日 08:19:04 +00:00Commented Jan 31, 2016 at 8:19
2 Answers 2
See my response on this related question : I advise you on using supervisord
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.
-
very bad advice, because python scripts don't crash for no reason, and you'd better find the reason than hide it away.lenik– lenik2014年11月07日 10:21:56 +00:00Commented 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.Hari Sundararajan– Hari Sundararajan2014年11月07日 11:57:49 +00:00Commented Nov 7, 2014 at 11:57