Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

Basically you want to make use of the variable $?. This variable contains the return status of the last run command.

% /bin/false
% echo $?
1
% /bin/true
% echo $?
0

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).

% /bin/false && echo true || echo false
false
% /bin/true && echo true || echo false
true

Something like this:

% rsync \
    && (echo "succeed" | mail -s "succeed" <email>) \
    || (echo "failure" | mail -s "failure" <email>)
share|improve this answer
    
Thank you. Exactly what was needed. Utilized the propose variable, however, ran a short if statement after each cycle as there are multiple rsyncs actions and I needed individual status. –  Marcos Sander Feb 3 '13 at 22:30
    
Glad it helped. Yeah was showing you the technique, expand it as needed. Please upvote and access this answer if it's the most appropriate one! –  slm Feb 3 '13 at 22:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.