Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What would be a good solution to make a failover pair that has multiple cronjobs running avoid running the processes twice?

Either in failover, or by dividing the jobs between them, and of course being able to take over all jobs when one of the nodes fails.

I could write a script for this, but someone must have fixed this already, or is it such an uncommon request?

share|improve this question
To add to the question, what if the master fails during the execution of a job? Is there any way for the slave to notice it and reexecute it? – skinp Jan 27 '11 at 17:08
up vote 5 down vote accepted

If you have some kind of cluster solution to provide high availability, it is fairly straightforward to do this.

We set up all cron jobs on both (or all) nodes in a cluster. Each job starts by executing a small script which works out if this is the master node in the cluster or not (by checking for the cluster floating IP address). If this is not the master node, the check_for_master script exits with an error which causes the whole cron job to fail. If this node is the master, the check_for_master script runs the job as normal.

The contents of the check_for_master script really depend on which cluster software you are using and the OS you are running.

For example, here's a sample crontab entry:

00 04 * * * /usr/local/bin/check_for_master /usr/local/bin/program-you-want-to-run >/tmp/logfile.out 2>&1
share|improve this answer

You can use 'rcron' for this particular problem. Rcron offers you with a state file, which simply says "active" or "passive", and if it's active your cron will run on a certain machine. If state file is set to passive it won't run. Simple as that.

Your cron jobs that used to look like:

* * * * *    root    echo "foobar"

will need to be changed to:

* * * * *    root    rcron echo "foobar"

and that's it.

share|improve this answer

I think what you're looking for is rather some job scheduling system (as used in compute clusters). Of course you won't need the most stuff of it, but tuning cron to do the things you require is probably more work than evaluating such a tool.

share|improve this answer
Unfortunately I don't have the luxury of changing the system around, but I will keep this in mind when a new cluster is setup for this purpose. – Bas Grolleman Jan 28 '11 at 12:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.