Skip to content

Instantly share code, notes, and snippets.

@ankitdbst
Created February 18, 2022 01:05
Show Gist options
  • Save ankitdbst/7523e77e0f08842186ede8116478cabb to your computer and use it in GitHub Desktop.
Save ankitdbst/7523e77e0f08842186ede8116478cabb to your computer and use it in GitHub Desktop.
Run cron with SNS notification on failure
#!/bin/sh
#
# Usage: Add below to crontab -e
# * * * * run.sh script
#
# This would run npm --prefix /path/to/app run script
if [ -z "$1" ]
then
echo "No script specified"
exit;
fi
cmdoutput=$(/usr/bin/npm --prefix /path/to/app run $1 2>&1)
exitcode=$?
echo "$cmdoutput"
if [ $exitcode -ne 0 ]; then
aws sns publish \
--topic-arn "${AWS_NOTIFY_TOPIC}" \
--message "cron $1 failed with error: ${exitcode}\n\n${cmdoutput}"
exit $exitcode
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment