Cron jobs in cascade array

If anyone it's like me and work even once with IBM Mainframe batch jobs, then it's possible that you know CONTROL-M, that is a tool to schedule jobs not only in a date and time fashion but also in terms of conditionals base on success or failures of other jobs

So i wonder if in a linux machine if this possible with the cron server. The only way I know if that a cron job must run at certain date and time, now I want to run in a chain of other cron jobs. Say so that when one finish triggers the other one to start and so.

It's there a way to accomplish this?

Best, Demian


Solution 1:

The standard approach to running one command if another fails is to check the first command's exit status for anything other than success and run if so.

Here's one recently seen hacktastic example:

### Start SSH if it stops. 
*/8 * * * * /usr/lib/nagios/plugins/check_ssh -p 22 10.0.0.34 > /dev/null || /etc/init.d/sshd restart

The above will only run the "/etc/init.d/sshd restart" command if the initial command fails. That's exactly what you're looking for. I imagine it's only on that mainframe because it duplicates simple conditional statements that exist in just about every shell.

If you have a whole bunch of "jobs" that should be run ONLY if the first fails...then just place all those jobs in a shell script and the initial command in the cron:

# Run my cool script if "somecommand" fails.
*/8 * * * * /usr/bin/somecommand || /home/foo/bin/mycoolscript.sh

For bash the && and || built-ins will do.