cron-mailonfailure: workhorse of the cronjob world
[cron-utils.git] / cron-mailonfailure
blobc20638f93fdb99588440735d75d4abdea888a2d2
1 #!/bin/bash
3 # cron-mailonfailure:
4 # Run the provided command, capturing stdin&stdout in a tmpfile.
5 # If the command succeeds, discard, exit.
6 # If the command errors out, send an email with a good subject.
8 # Author: Martin Langhoff <martin.langhoff@gmail.com>
9 # License: GPLv2
11 mailto=$1
12 shift
14 # logfilename
15 logfile=$(mktemp)
16 cmdstring="$@"
17 echo "Running $cmdstring" > ${logfile}
18 "$@" &>>${logfile}
19 exitcode=$?
20 if [[ $exitcode -ne 0 ]]; then
21 echo "Exited with $exitcode" >> ${logfile}
22 cat ${logfile} | mailx -s "$(hostname -s): Error running $cmdstring" "${mailto}"
25 rm ${logfile}
27 # propagate exitcode up the call chain
28 # in case our caller cares about success/failure
29 exit $exitcode