I have a simple script on my home server to sync data to my backup drive to be stored.
Right now it runs multiple rsyncs one after another.
Sometimes rsync fails on one or two of them. When this happens, I want it to email me saying it failed, I already have it email me a report, but, I want a way to know if it fails.
If Rsync exits with a non 0 code it means failure. So I would need a simple bit of code to check the rsync status and if it's not 0, allow me to execute an action.
Does anyone have any tips on how i would accomplish this? Thank you.
Basically you want to make use of the variable
$?
. This variable contains the return status of the last run command.You can use these constructs to run
rsync
and send you mail if it succeeds (echo true) or send you mail if it fails (echo false).Something like this:
for using in script I prefer this universal construction:
You can add using sendmail at this function or add MAILTO in your crontab
A suggestion/ optimization, to preceeding anwers:
If it is crucial, i'd make the script confirm an OK instead of a fail, for example with a
wget "http://yourpage.com/callhome.php?device_id=xxx"
and let the webserver deal with checking if there was no call home. (first DB entry with the wget call, then cron check if there was an entry for that machine) and react in any/many ways (mail, sms/text, ...)
because there are so many reasons why a mail never reaches its target. It's a lot more work, I agree...