检查rsync命令是否成功运行 [英] Check if rsync command ran successful

查看:163
本文介绍了检查rsync命令是否成功运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下bash脚本每小时执行一次文件夹的rsync:

The following bash-script is doing a rsync of a folder every hour:

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

但是我发现我的Pi断开了与互联网的连接,因此rsync失败了.因此,它执行了以下命令,删除了该文件夹.如何确定rsync命令是否成功(如果成功),则可能会删除该文件夹.

But I found out that my Pi disconnected from the internet, so the rsync failed. So it did the following command, deleting the folder. How to determine if a rsync-command was successful, if it was, then it may remove the folder.

推荐答案

通常,任何Unix命令如果成功运行,都应返回0,在其他情况下,则应返回非0.

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

在man rsync中查找可能与您的情况相关的退出代码,但是我会这样:

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

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

只有一切正常,这将rm和echo完成.

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/bash
rsync -r -z -c /home/pi/queue root@server.mine.com:/home/foobar
if [ "$?" -eq "0" ]
then
  rm -rf rm /home/pi/queue/*
  echo "Done"
else
  echo "Error while running rsync"
fi

请参见 man rsync ,第退出值

这篇关于检查rsync命令是否成功运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆