【发布时间】:2016-06-10 13:09:04
【问题描述】:
我有一个执行备份的 shell 脚本。我将此脚本设置在 cron 中,但问题是备份很重,因此可以在第一个 rsync 结束之前执行第二个 rsync。 我想在脚本中启动 rsync 然后获取 PID 并编写一个文件,该脚本检查进程是否存在(如果该文件存在或不存在)。 如果我将 rsync 放在后台,我会得到 PID,但我不知道如何知道 rsync 何时结束,但是,如果我设置 rsync(无背景),我无法在进程完成之前获得 PID,所以我不能写文件带有 PID。
我不知道“拥有 rsync 控制”以及何时完成的最佳方式是什么。
我的脚本
#!/bin/bash
pidfile="/home/${USER}/.rsync_repository"
if [ -f $pidfile ];
then
echo "PID file exists " $(date +"%Y-%m-%d %H:%M:%S")
else
rsync -zrt --delete-before /repository/ /mnt/backup/repositorio/ < /dev/null &
echo $$ > $pidfile
# If I uncomment this 'rm' and rsync is running in background, the file is deleted so I can't "control" when rsync finish
# rm $pidfile
fi
谁能帮帮我?!
提前致谢!! :)
【问题讨论】:
-
@user2181624 我不知道我要等多久:S 我认为这不是最好的方法。
-
您无需知道要等待多少时间。查看 bash 手册页(因为它很长,(并且单词 wait 出现了无数次)转到手册页的末尾,向后搜索“SHELL BUILTIN”,然后向前搜索“等待”。