Usage

clustercron –help

Clustercron, cluster cronjob wrapper.

Usage:
    clustercron [options] elb <load_balancer_name> [<cron_command>]
    clustercron [options] alb <target_group_name> [<cron_command>]
    clustercron -h | --help
    clustercron --version

Options:
    -v --verbose  Info logging. Add extra `-v` for debug logging.
    -s --syslog   Log to (local) syslog.
    -c --cache    Cache output from master check.
    -o --output   Output stdout and stderr from <cron_command>.

Clustercron is cronjob wrapper that tries to ensure that a script gets run
only once, on one host from a pool of nodes of a specified loadbalancer.

Without specifying a <cron_command> clustercron will only check if the node
is the `master` in the cluster and will return 0 if so.

Command line examples

Clustercron can be run from command line for debugging.

For Clustercron ELB a Load Balancer Name. must be specified.

For Clustercron ALB a Target Group Name. must be specified.

Without a command specified clustron will test is the node is master and return 0 if that is the case:

$ clustercron elb mylbname
$ echo $?
0

On a node not determined as ‘master’:

$ clustercron elb mylbname || echo "I'm not master"
I'm not master

On a node determined as ‘master’:

$ clustercron elb mylbname && echo "I'm master"
I'm master

With options -v or –verbose clustercron will output vebose info to console.

Check if node is determined as master with verbose (info) output:

$ clustercron -v elb mylbname

Check if node is not determined as master with verbose (info) output:

$ clustercron -v elb mylbname
INFO     clustercron.lb : Instance ID: i-05cc16d1d054104f2
INFO     clustercron.elb : Healty instances: i-05cc16d1d054104f2, i-0f13be692f5f35b5e

With options -s or –syslog and -v or –verbose clustercron will only output to syslog. ELB example:

$ clustercron -v -s elb mylbname echo test
$ sudo tail /var/log/messages
Jul 31 13:58:24 ip-172-31-7-231 journal: clustercron.lb [31512]: Instance ID: i-05cc16d1d054104f2
Jul 31 13:58:24 ip-172-31-7-231 journal: clustercron.elb [31512]: Healty instances: i-05cc16d1d054104f2, i-0f13be692f5f35b5e
Jul 31 13:58:24 ip-172-31-7-231 journal: clustercron.main [31512]: run command: echo test
Jul 31 13:58:24 ip-172-31-7-231 journal: clustercron.main [31512]: stdout: test
Jul 31 13:58:24 ip-172-31-7-231 journal: clustercron.main [31512]: stderr:
Jul 31 13:58:24 ip-172-31-7-231 journal: clustercron.main [31512]: returncode: 0

With options -s or –syslog and -v or –verbose clustercron will only output to syslog ALB example:

$ clustercron -v -s alb mytargetgroupname echo test
Jul 31 14:04:53 ip-10-0-2-129 journal: clustercron.lb [31204]: Instance ID: i-05d7670fb9114c58e
Jul 31 14:04:53 ip-10-0-2-129 journal: clustercron.alb [31204]: Healty instances: i-05d7670fb9114c58e, i-0b5be35c81d1b50d4
Jul 31 14:04:53 ip-10-0-2-129 journal: clustercron.main [31204]: run command: echo test
Jul 31 14:04:53 ip-10-0-2-129 journal: clustercron.main [31204]: stdout: test
Jul 31 14:04:53 ip-10-0-2-129 journal: clustercron.main [31204]: stderr:
Jul 31 14:04:53 ip-10-0-2-129 journal: clustercron.main [31204]: returncode: 0

By default clustercron will not output stdout and stderr generated by the ‘cron-command’:

$ clustercron elb mylbname echo test
$

With option -o or –output ` clustercron will output stdout and stderr generated by the ‘cron-command’ (when the node is determined as ‘master’):

$ clustercron -o elb mylbname echo test
test

Output redirection with options -o or –output:

$ clustercron -o elb mylbname echo test > /tmp/output
$ cat /tmp/output
test

stdout and stderr separated with redirection:

$ clustercron -o elb mylbname cat non_existing_file  1>/tmp/output 2>/tmp/error
$ cat /tmp/output
$ cat /tmp/error
cat: non_existing_file: No such file or directory

Be aware that redirection takes place on clustercron itself, not on the ‘cron-command’. So redirection will also take place when the node is not determined as ‘master’.

When a node is not determind as ‘master’ an empty file will be created when redirecting output from clustercron:

$ clustercron -o elb clustercrontest echo test  > /tmp/output
$ cat /tmp/output

When redirection is only wanted on the ‘cron-command’ when clustercron determined a node as ‘master’ a ‘cron-command’ could be wrapped:

$ cat wrapped_cron_command.sh
#!/bin/sh
echo test > /tmp/output

On a node determined as ‘master:

$ clustercron -o elb clustercrontest wrapped_cron_command.sh
$ cat /tmp/output
test

On a node not determined as ‘master:

$ clustercron -o elb clustercrontest wrapped_cron_command.sh
$ cat /tmp/output
cat: /tmp/output: No such file or directory

Cron entry example

Every day at 5 min to midnight run the command logger “clustercron run” on the node that will be picked master . Log with level INFO to syslog:

55 23 * * * /<path>/<to>/<virtualenv_name>/bin/clustercron -v -s elb <lb name> logger "clustercron run"

Caching

Clustercron’s master selection can be cached with the options -c or –cache:

$ clustercron -c elb mylbname echo test

By default the cache will stored in /tmp/clustercron_cache.json and expire after 59 seconds. Clustercron will lock the cache file and tries to by default 20 times when the file is locked.

The defaults for caching can only be altered in Clustercron’s configuration file.

See Configuration for more information.