Run cron jobs in a standardized environment with logs and locking
ohmycron-- Cron simplified
Frequently when debugging cron jobs, one finds that:
One needs to use locks to keep more than one copy of frequently running jobs from running at the same time.
One wishes to load the user environment --
/etc/profileas well as RC files in
HOME.
Generally, one would
cdto
HOMEfor application specific users.
The path should include
/usr/local/bin.
The output of the cron job should be logged to Syslog, which both prevents
cronfrom raising errors about mailers and allows you to see what went wrong if something did.
ohmycrondoes all this and more, managing locks with a POSIX API, so that the OS takes care of cleaning up locks for failed proceseses.
ohmycronas a wrapper
Prepending
ohmycronto the commands in your crontab keeps your crontab clean and transparently adds logging, locking and environment loading.
* * * * * root ohmycron sleep 10 * * * * * root ohmycron --tag update:ohmycron -- curl -sSfL 'https://raw.githubusercontent.com/instacart/ohmycron/master/ohmycron' -o /usr/local/bin/ohmycron
ohmycronas the cron shell
Setting
SHELL=/usr/local/bin/ohmycron(or another path if you have installed
ohmycronelsewhere) transparently adds locks, logging and environment setup to all the jobs in a cron file. (Tasks are actually run with Bash.) It is does something the wrapper can't do, too: support multi-statement commands which use the shell operators
&&,
|,
||and so forth.
SHELL=/usr/local/bin/ohmycron * * * * * root sleep 10 * * * * * root : update:ohmycron ; curl -sSfL 'https://raw.githubusercontent.com/instacart/ohmycron/master/ohmycron' -o /usr/local/bin/ohmycron
You can explicitly name a cron job with a Bash "no-op comment":
: ;. (In Bash,
:is a no-op; the arguments to the no-op command are ignored.)
sudo curl -sSfL 'https://raw.githubusercontent.com/instacart/ohmycron/master/ohmycron' -o /usr/local/bin/ohmycron sudo chmod a+rx /usr/local/bin/ohmycron