Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Define the location of the PID file
- PIDFILE="/tmp/rsync_sync_task.pid"
- # 1. Check if the PID file exists
- if [ -f $PIDFILE ]; then
- OLD_PID=$(cat $PIDFILE)
- echo "Found existing process ID: $OLD_PID. Terminating it..."
- # Kill the specific PID.
- # The '|| true' ensures the script continues even if the process is already gone.
- kill $OLD_PID 2>/dev/null || true
- # Optional: Wait a moment to ensure the port/file handles are released
- sleep 2
- fi
- # 2. Record the current PID ($$) into the file before starting rsync
- echo $$ > $PIDFILE
- echo 'Starting rsync...'
- echo '=============================================='
- # 3. Run your rsync command
- rsync -avp --append-verify --partial --progress --exclude '/incomplete' --exclude '**/incomplete' -e ssh [email protected]:~/sync/ /destination/ >> syncyes.log
- date >> syncyes.log
- echo '==================================' >> syncyes.log
- echo 'rsync done.'
- # 4. Clean up the PID file when finished
- rm $PIDFILE
Advertisement