我有一個執行備份的shell腳本。我在cron中設置了這個腳本,但問題是備份非常繁重,因此可能在第一次結束之前執行第二次rsync。 我想在腳本中啓動rsync,然後獲得PID並編寫一個腳本檢查進程是否存在的文件(如果此文件存在或不存在)。 如果我把rsync放在後臺,我得到了PID,但我不知道如何知道rsync何時結束,但是如果我設置了rsync(無後臺),那麼在進程結束之前我無法獲得PID,因此我無法寫入一個文件惠特PID。從shell腳本中獲取背景rsync和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
任何人可以幫助我嗎?
在此先感謝! :)
使用wait commamd – user2181624
@ user2181624我不知道需要等多少時間:S我認爲不是最好的方法。 – jask
你不需要知道需要等待多少時間。看到bash手冊頁(因爲它很長,(等待這個詞的時間似乎超過了幾十億次)轉到手冊頁的末尾,向後搜索'SHELL BUILTIN',然後向前搜索'wait'。 – Cwissy