I have a python program which, from time to time, crashes with a number of different exceptions usually due to network issues. The only way to get it to work is to restart it. How would I write a shell script to run the process, continually check if any of the output says Error
and if so end the process and restart the command (let's call it ./command.sh
)?
It would be fine for this to be in any other language (python/perl etc).
Since python exits with an return code != 0 when it throws an exception, you can just run this shell script:
Of course if you just want to run the command all the time, just write:
(this assumes
./command.sh
doesn't fork and exit)I did something similar to recover ted (Torrent Episode Downloader) from its regular crashes. Below is the script I used. Will explain it in further edits as I go.
Edit: How it works
Ted has a log file. Since it checks for new episodes every half an hour, there is a timestamp in that log, that's not older than half an hour. Of course the timestamp is not there if ted crashed in the mean time.
Edit: How that might help you
You could redirect your problematic program's output into a file. Write a script similar to mine, that checks this file. If it finds the bad string there, restarts the application (and removes the output file). You can ran the script in an endless loop or use cron. I chose to use cron so I have less process pollution and the checks don't have to be incredibly frequent anyway.
In my case there was still an active process for ted after the crash, but ted wasn't doing anything. Kills it's process, then you can of course skip the pid-Parts.
P.S. It might or might not be a bad piece of code. There might be a much better way, but it works, and I put it together in a very short time.