Last active
November 20, 2023 07:22
-
-
Save jonocarroll/5e11fb561a36167bad7687b72c8e0b2f to your computer and use it in GitHub Desktop.
Use crontab to monitor the failure of a script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
LOG_FILE="/Users/username/output.log" | |
NOTIFIED_FILE="/Users/username/notified.txt" | |
EMAIL="[email protected]" | |
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*/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