Skip to content

Instantly share code, notes, and snippets.

@danwald
Last active February 12, 2017 19:48
Show Gist options
  • 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 [email protected] $from_name "Failed to run $lockfile @ $dt cmd:${@:2}"
exit 1
fi
$sendmail [email protected] $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