I have this small script which basically do rsync of files between to remote server. ssh key has been generated for authentication.

only this is present in the script

rsync /root/Desktop/cpfies/files/*.xls user@host:root/Desktop/cpfies/moved

basically what I want to create is a log method for this script not to fancy or anything ...only this... if the connection is lost and the file is not sent it comes in my log saying "File not sent connection not established"

also if the files has been sent it shows me in the logs successfully sent.

or if the host or the user is wrong it show in my log.

Thanks and Regards, Sagar Mandal

  • Is the normal output of rsync okay? Then you can just redirect it (including stderr) to your logfile.
    – pLumo
    Apr 9 '20 at 5:57

If you want to log only unsuccessful attempts, you can get rsync return value by looking into $?, which is the return value of the last script. rsync returns 0 on success and non zero on different errors (https://lxadm.com/Rsync_exit_codes). I would to it in that way:


ret_text=$({rsync /root/Desktop/cpfies/files/*.xls user@host:root/Desktop/cpfies/moved} 2>&1)

if [ $? -ne 0 ]; then
    echo rsync error code $? on $(date)
    echo rsync output follows
    echo $ret_text

You can put that into a script and when you run it, redirect its output into a log file like that script >> log_file, or you can just call script and add >> log_file after each echo command in the script.

If you want to log also successful attempts than you do

rsync /root/Desktop/cpfies/files/*.xls user@host:root/Desktop/cpfies/moved &>> log_file


rsync --log-file=log_file /root/Desktop/cpfies/files/*.xls user@host:root/Desktop/cpfies/moved

I would also suggest to increase the amount of information that rsync produces by adding -v or -vv. It is also possible to use -vvv or even more, but manual says it is for debugging purposes.

  • Thank you so much I'll try this and will let you know :-)
    – user404480
    Apr 10 '20 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy