7

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?

Bas Grolleman
  • 228
  • 2
  • 5
  • 1
    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

3 Answers3

6

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
dr-jan
  • 424
  • 7
  • 16
2

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.

ricmarques
  • 1,124
  • 1
  • 13
  • 23
Jakov Sosic
  • 5,267
  • 4
  • 24
  • 35
-1

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.

weeheavy
  • 4,089
  • 1
  • 28
  • 41
  • 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