Skip to content

Instantly share code, notes, and snippets.

@jonocarroll
Last active November 20, 2023 07:22
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 jonocarroll/5e11fb561a36167bad7687b72c8e0b2f to your computer and use it in GitHub Desktop.
Save jonocarroll/5e11fb561a36167bad7687b72c8e0b2f to your computer and use it in GitHub Desktop.
Use crontab to monitor the failure of a script
#!/bin/bash
LOG_FILE="/Users/username/output.log"
NOTIFIED_FILE="/Users/username/notified.txt"
EMAIL="username@example.com"
# Read the contents of the notified file, if it exists
notified=$(cat "$NOTIFIED_FILE" 2>/dev/null)
# If the notified file doesn't exist, assume no previous notification
if [ -z "$notified" ]; then
# Detect errors
new_errors=$(awk '/Execution halted/ {print $1}' "$LOG_FILE")
# If there are new errors, send a notification
if [ -n "$new_errors" ]; then
# Send a notification via email
mail -s "Cron Job Failure" $EMAIL <<EOF
There was an error in the cron job. Check the log file: $LOG_FILE
Log file contains:
$(cat $LOG_FILE)
This message will not be sent again until $NOTIFIED_FILE is cleared.
EOF
# Update the notified file
echo "1" > "$NOTIFIED_FILE"
fi
fi
#!/bin/bash
LOG_FILE="/Users/username/output.log"
NOTIFIED_FILE="/Users/username/notified.txt"
TOPIC="example_topic_changeme"
# Read the contents of the notified file, if it exists
notified=$(cat "$NOTIFIED_FILE" 2>/dev/null)
# If the notified file doesn't exist, assume no previous notification
if [ -z "$notified" ]; then
# Detect errors
new_errors=$(awk '/Execution halted/ {print $1}' "$LOG_FILE")
# If there are new errors, send a notification
if [ -n "$new_errors" ]; then
# Send a notification via ntfy
curl \
-T $LOG_FILE \
-H "Title: Cron Job Failure" \
-H "Tags: warning" \
-H "Filename: $LOG_FILE" \
"ntfy.sh/$TOPIC"
# Update the notified file
echo "1" > "$NOTIFIED_FILE"
fi
fi
*/2 * * * * /usr/local/bin/Rscript -e "R.version; cat('Execution halted\n')" > /Users/username/output.log 2>&1
*/5 * * * * /Users/username/check_errors.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment