下面的bash脚本每小时执行一次文件夹的rsync:
#!/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-命令是否成功,如果成功,则可能会删除文件夹。
发布于 2014-06-27 14:40:49
通常,任何Unix命令如果运行成功,将返回0,而在其他情况下将返回非0。
在man rsync中查找可能与您的情况相关的退出代码,但我会这样做:
#!/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。
另一种方法是使用$?变量,该变量始终是前一个命令的返回码:
#!/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的EXIT VALUES一节
发布于 2014-06-27 14:37:56
需要检查rsync的退出值
#!/bin/bash
rsync -r -z -c /home/pi/queue root@server.mine.com:/home/foobar
if [[ $? -gt 0 ]]
then
# take failure action here
else
rm -rf rm /home/pi/queue/*
echo "Done"
fi
此处为结果代码集:http://linux.die.net/man/1/rsync
发布于 2017-01-25 10:18:39
这是个老问题,但我很惊讶没有人给出简单的答案:
您可以使用--remove-source-files rsync选项。
我想这正是你所需要的。
从手册页:
--remove-source-files sender removes synchronized files (non-dir)
仅删除rsync已完全成功传输的文件。
如果不熟悉rsync,很容易混淆--delete选项和--remove-source-files选项。--delete选项删除目标端上的文件。更多信息请点击此处:https://superuser.com/questions/156664/what-are-the-differences-between-the-rsync-delete-options
https://stackoverflow.com/questions/24454391
复制相似问题