Skip to content

Instantly share code, notes, and snippets.

@danwald
Last active February 12, 2017 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danwald/06ae49cb5a64ca1e90655d950ff02750 to your computer and use it in GitHub Desktop.
Save danwald/06ae49cb5a64ca1e90655d950ff02750 to your computer and use it in GitHub Desktop.
Flock commands so scripts/cronjobs don't overrun. Send email on failure.
#!/bin/bash
export from_name=''
lockfile=$1
flock=`which flock`
#uses mandrill-sendmail via @blackdotsh's https://raw.githubusercontent.com/blackdotsh/mandrillapp-cli/master/sendmail.sh
sendmail=$HOME/msendmail.sh
dt=`date`
$flock -xn "/tmp/$lockfile" echo "${@:2}"
if [ $? -eq 1 ]
then
echo "$lockfile not executed @ $dt. Already running"
$sendmail danwald79@gmail.com $from_name "Failed to run $lockfile @ $dt cmd:${@:2}"
exit 1
fi
$sendmail danwald79@gmail.com $from_name "Ran $lockfile @ $dt cmd:${@:2}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment