I have a shell script that does a backup. I set this script in a cron but the problem is that the backup is heavy so it is possible to execute a second rsync before the first ends up. I thought to launch rsync in a script and then get PID and write a file that script checks if the process exist or not (if this file exist or not). If I put rsync in background I get the PID but I don’t know how to know when rsync ends up but, if I set rsync (no background) I can’t get PID before the process finish so I can’t write a file whit PID.
I don’t know what is the best way to “have rsync control” and know when it finish.
My script
JavaScript
x
#!/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
Can anybody help me?!
Thanks in advance !! 🙂
Advertisement
Answer
JavaScript
# 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
- pidof finds the pid of a process
- -x tells it to look for scripts too
- ${sScriptName} is just the name of the script…you can hardcode this
- wc -w returns the word count by words
- -gt 2 no more than one instance running (instance plus 1 for the pidof check)
- if more than one instance running then exit script
Let me know if this works for you.