Skip to content

Instantly share code, notes, and snippets.

@preaction
Created June 18, 2012 17:56
Show Gist options
  • Select an option

  • Save preaction/2949713 to your computer and use it in GitHub Desktop.

Select an option

Save preaction/2949713 to your computer and use it in GitHub Desktop.
output mail digest system
# digest.sh
# Create an e-mail digest of all output, STDOUT and STDERR, and send it
# when given the proper command.
# Usage:
# digest.sh add <job_name> <command>
# # Send an e-mail and reset the digest
# digest.sh send <job_name>
# Environment:
# DIGEST_DIR: The directory to store digests in
# MAILTO: The e-mail to send digests to
DIGEST_DIR=${DIGEST_DIR:-$HOME/var/spool/digest}
if [ ! -d "$DIGEST_DIR" ]; then
echo "DIGEST_DIR $DIGEST_DIR does not exist!"
exit 2
fi
add(){
JOB_NAME=$1
shift
COMMAND=$*
exec $COMMAND > $DIGEST_DIR/$JOB_NAME.$$ 2>&1
}
send(){
if [[ "$MAILTO" == "" ]]; then
echo "MAILTO must contain an e-mail address!"
exit 1
fi
JOB_NAME=$1
TEMPMAIL=/tmp/digest.$$.mail
ALL_FILES=""
for F in $DIGEST_DIR/$JOB_NAME.*; do
ALL_FILES="$ALL_FILES $F"
done
if [[ "$ALL_FILES" == "" ]]; then
exit 0; # Nothing to do
fi
cat - $ALL_FILES > $TEMPMAIL <<ENDMAIL
To: $MAILTO
From: $USER@$HOSTNAME
Subject: Cron <$USER@$HOSTNAME> Digest output for $JOB_NAME
ENDMAIL
# Empty line before ENDMAIL is important to separate smtp mail header from body
sendmail $MAILTO < $TEMPMAIL
rm $TEMPMAIL
rm $ALL_FILES
}
COMMAND=$1
shift
JOB_NAME=$1
shift
if [[ "$COMMAND" == "add" ]]; then
add $JOB_NAME $*
elif [[ "$COMMAND" == "send" ]]; then
send $JOB_NAME $*
else
echo "Must specify either 'add' or 'send' as first argument!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment