Status checker
If e.g. you want to monitor the status of a cronjob, but don't want to be warned everytime it fails, you could use something like this, which I am using for a backup on a laptop.
The backup is often interrupted by shutting down the computer, so I only want to be notified if it hasn't been successfull for 2 weeks in a row:
Storing states
In the backup script
backup_info_path=$HOME/.backup
last_warned_file=$backup_info_path/last_warned.txt
last_ok_file=$backup_info_path/last_ok.txt
#... backup code
#... rsync command
status=$?
if [ $status -eq 0 ];then
date +%Y%m%d > $last_ok_file
date +%Y%m%d > $last_warned_file
fi
In the monitor script
#!/bin/bash
backup_info_path=$HOME/.backup
last_ok_file=$backup_info_path/last_ok.txt
last_warned_file=$backup_info_path/last_warned.txt
current_ts=$(date +%s)
last_ok=0
if [ -f $last_ok_file ];then
last_ok=$(date +%s -d $(cat $last_ok_file))
fi
last_warned=0
if [ -f $last_warned_file ];then
last_warned=$(date +%s -d $(cat $last_warned_file))
fi
delta_ok=$[ ( $current_ts - $last_ok ) / 86400 ]
delta_warned=$[ ( $current_ts - $last_warned ) / 86400 ]
if [ $delta_ok -lt 14 ];then
exit 0;
fi
if [ $delta_warned -lt 7 ];then
exit 0;
fi
xmessage "Backup not successful for $delta_ok days!"
date +%Y%m%d > $last_warned_file