As per the title:
What's the best way of setting up a cron job to check that a long-running process is still going and if not, start it?
If I start a long-running process in cron, is it going to block? or does cron fork the process as an independent child?
Thanks!
Best Answer
A simple approach is to have a straightforward script that checks whether the process is running or not, and then restart it when necessary.
(Sometimes it's best to actually validate that the process is running by means of a 'dummy transaction', e.g. to verify an SMTP process you might make a TCP port connection and check that it responds correctly.)
But do watch out for differences in your environment between you as an interactive user, and when cron(8) runs your script.
To answer the second bit of your question:
cron(8) will fork to execute a cron job, but unless your script or process 'detaches', cron will maintain it as a child process until it exits (that's how cron is able to collect all output from stderr, and send it via email.)
But, I think you were thinking, could you actually run the long-running process from cron? If you do that, you need to make sure that it can only start one-copy of itself, and that it will quickly exit if it is already running.
Better solutions for keeping long-running processes running - if you're only worried about an exit or crash