Skip to content

Instantly share code, notes, and snippets.

@afirth
Last active April 28, 2016 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save afirth/095b6a2b6698257b56ae to your computer and use it in GitHub Desktop.
Save afirth/095b6a2b6698257b56ae to your computer and use it in GitHub Desktop.
cron handlers
#!/bin/bash
#don't write shell scripts without this...
set -e
cd /my/folder
#Use timeout and flock to run your unreliable script every minute. Run only one copy. Useful against REST APIs for example
timeout --signal=KILL 59 flock -n /tmp/mycommand.lock -c 'mystupid long command'
#Log stdout and stderr. Discard stdout so cron doesn't mail you about it, but still receive mail for errors.
#e.g. in perl print wil generate logs but not mail, while warn and die will generate both
/usr/bin/perl ./foo.pl arg1 arg2 > >(logger -t foo) 2> >(logger -s -t foo)
#combine the two
timeout --signal=KILL 59 flock -n /tmp/mycommand.lock -c '/usr/bin/perl ./foo.pl arg1 arg2 > >(logger -t foo) 2> >(logger -s -t foo)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment