Check if rsync command ran successful Check if rsync command ran successful shell shell

Check if rsync command ran successful


Usually, any Unix command shall return 0 if it ran successfully, and non-0 in other cases.

Look at man rsync for exit codes that may be relevant to your situation, but I'd do that this way :

#!/bin/bashrsync -r -z -c /home/pi/queue root@server.mine.com:/home/foobar && rm -rf rm /home/pi/queue/* && echo "Done"

Which will rm and echo done only if everything went fine.

Other way to do it would be by using $? variable which is always the return code of the previous command :

#!/bin/bashrsync -r -z -c /home/pi/queue root@server.mine.com:/home/foobarif [ "$?" -eq "0" ]then  rm -rf rm /home/pi/queue/*  echo "Done"else  echo "Error while running rsync"fi

see man rsync, section EXIT VALUES


you need to check the exit value of rsync

#!/bin/bashrsync -r -z -c /home/pi/queue root@server.mine.com:/home/foobarif [[ $? -gt 0 ]] then   # take failure action hereelse   rm -rf rm /home/pi/queue/*   echo "Done"fi

Set of result codes here:http://linux.die.net/man/1/rsync


Old question but I am surprised nobody has given the simple answer:
   Use the --remove-source-files rsync option.
I think it is exactly what you need.

From the man page:

--remove-source-files   sender removes synchronized files (non-dir)

Only files that rsync has fully successfully transferred are removed.

When unfamiliar with rsync it is easy to be confused about the --delete options and the --remove-source-files option. The --delete options remove files on the destination side. More info here: https://superuser.com/questions/156664/what-are-the-differences-between-the-rsync-delete-options