If you're going to do this, you need to deal with the possibility that the script will die unceremoniously, leaving the lock file behind. This adds a trap, makes the lock file identify the machine, PID, and date/time the lock was asserted, and examines that information and attempts to determine whether the lock is really valid rather than blindly assuming that it is.<br>
<br><div style="font-family: courier new,monospace;" class="gmail_quote"><b><font size="2">
#!/bin/bash<br>Lock=</font></b><b><font size="2">/some-where/rsync-is-running</font></b><br><b><font size="2">Log=</font></b><font size="2"><b>/some-where/rsync.log</b></font><br><b><font size="2">if [ -f $</font></b><b><font size="2">Lock</font></b><b><font size="2"> ]<br>
then</font><font size="2"> <br> read <$</font></b><b><font size="2">Lock</font></b><b><font size="2"> Machine PID RDate<br> if [ `uname -n` = "$Machine" ]<br> then<br> if [ "x`ps -ocmd= -p $PID`" = "x$0" ]<br>
then</font><br><font size="2">
echo "`date` - An instance of rsync is already running on this server: `</font>ps -fp $PID<font size="2">`" >>$Log<br> exit<br></font><font size="2"> else<br> echo "`date` Ignoring invalid lock entry: `cat </font></b><b><font size="2">$</font></b><b><font size="2">Lock</font></b><b><font size="2">`</font><font size="2">"<br>
fi #</font><font size="2">[ "x`ps -ocmd= -p $PID`" = "x$0" ]</font><br><font size="2">
else<br></font><font size="2">
echo "`date` - An instance of rsync appears to be already running on $Machine: </font><font size="2">`cat </font></b><b><font size="2">$</font></b><b><font size="2">Lock</font></b><b><font size="2">`</font><font size="2">" >></font></b><b><font size="2">$Log</font></b><br>
<b><font size="2">
exit<br>
</font><font size="2">
fi #</font><font size="2">[ `uname -n` = "$Machine" ]</font><br><font size="2">fi #</font></b><b><font size="2">[ -f $</font></b><b><font size="2">Lock</font></b><b><font size="2"> ]<br># If we make it this far, actually do it.<br>
</font></b><b><font size="2">echo "`date` - Starting synchronization" >> </font></b><b><font size="2">$Log</font></b><div style="background-color: rgb(255, 255, 255);"><b><font size="2">trap 'rm </font></b><b><font size="2">$</font></b><b><font size="2">Lock</font></b><b><font size="2">; exit' 1 2 3 4 5 6 7 8 10 11 12 13 14 15<br>
echo `uname -n` $$ `date` ></font></b><b><font size="2">$</font></b><b><font size="2">Lock</font></b></div><font size="2"><b><span style="background-color: rgb(255, 255, 255);"></span># ... your script here ...<br>echo "`date` - Finished synchronization" >> </b></font><b><font size="2">$Log</font></b><br>
<font size="2"><b>rm </b></font><b><font size="2">$</font></b><b><font size="2">Lock</font></b><font size="2"><br>
<br><font face="arial,sans-serif">You could probably even use rcmd to validate the alleged PID of the process on the other machine, if you wanted to go to the trouble. Or just have one server be the designated initiator of the rsync, which simplifies things greatly.</font><br>
</font></div><br><br>