How to make sure script is always running?

I have 4 scripts in crontab that start on server boot. After 12 hours or so, some throw an exception and stop running. At that point, I want my server to just start the script again, so each script is always running.

I can run:

ps ax | grep python

And I see which one it usually is that stops running.


Solution 1:

For me it looks that you are trying to solve a wrong problem here.

If your script acts as a daemon and needs to be running all the time, then it shouldn't crash periodically and expect to be restarted. Of course, unexpected crashes can happen, and yes, there are tools to monitor and restart services when they fail. However, if some service is crashing for you once or twice a day, then you better look what's wrong with it, instead of simply restarting it and pretending that it's fine.

If your script runs as a batch job and for some reason can't complete it's task, then again, it shouldn't just throw an exception, exit and wait to be restarted.

Find a reason why your script crashes and fix it. Use monitoring software to restart it, if unexpected failure happens, but not as a fix to a periodical crash.