[英]Background rsync and pid from a shell script
我有一個執行備份的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
有誰能夠幫助我?!
提前致謝 !! :)
測試pid文件的存在和正在運行的進程的狀態,如下所示:
#!/bin/bash
pidfile="/home/${USER}/.rsync_repository"
is_running =0
if [ -f $pidfile ];
then
echo "PID file exists " $(date +"%Y-%m-%d %H:%M:%S")
previous_pid=`cat $pidfile`
is_running=`ps -ef | grep $previous_pid | wc -l`
fi
if [ $is_running -gt 0 ];
then
echo "Previous process didn't quit yet"
else
rsync -zrt --delete-before /repository/ /mnt/backup/repositorio/ < /dev/null &
echo $$ > $pidfile
fi
希望這可以幫助!!!
# check to make sure script isn't still running
# if it's still running then exit this script
sScriptName="$(basename $0)"
if [ $(pidof -x ${sScriptName}| wc -w) -gt 2 ]; then
exit
fi
讓我知道這是否適合您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.