If you&#39;re going to do this, you need to deal with the possibility that the script will die unceremoniously, leaving the lock file behind.&nbsp; 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>
&nbsp;then</font><font size="2">&nbsp;&nbsp; <br>&nbsp; read &lt;$</font></b><b><font size="2">Lock</font></b><b><font size="2"> Machine PID RDate<br>&nbsp; if [ `uname -n` = &quot;$Machine&quot; ]<br>&nbsp;&nbsp; then<br>&nbsp;&nbsp;&nbsp; if [ &quot;x`ps -ocmd= -p $PID`&quot; = &quot;x$0&quot; ]<br>
&nbsp;&nbsp;&nbsp;&nbsp; then</font><br><font size="2">
 &nbsp;&nbsp; &nbsp;&nbsp; echo &quot;`date` - An instance of rsync is already running on this server:&nbsp; `</font>ps -fp $PID<font size="2">`&quot; &gt;&gt;$Log<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit<br></font><font size="2">&nbsp;&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;`date` Ignoring invalid lock entry: `cat&nbsp;</font></b><b><font size="2">$</font></b><b><font size="2">Lock</font></b><b><font size="2">`</font><font size="2">&quot;<br>

&nbsp;&nbsp;&nbsp; fi #</font><font size="2">[ &quot;x`ps -ocmd= -p $PID`&quot; = &quot;x$0&quot; ]</font><br><font size="2">
&nbsp;&nbsp; else<br></font><font size="2">
 &nbsp;&nbsp; &nbsp;&nbsp; echo &quot;`date` - An instance of rsync appears to be already running on $Machine: </font><font size="2">`cat&nbsp;</font></b><b><font size="2">$</font></b><b><font size="2">Lock</font></b><b><font size="2">`</font><font size="2">&quot; &gt;&gt;</font></b><b><font size="2">$Log</font></b><br>
<b><font size="2">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit<br>
</font><font size="2">
&nbsp; fi #</font><font size="2">[ `uname -n` = &quot;$Machine&quot; ]</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 &quot;`date` - Starting synchronization&quot; &gt;&gt; </font></b><b><font size="2">$Log</font></b><div style="background-color: rgb(255, 255, 255);"><b><font size="2">trap &#39;rm&nbsp;</font></b><b><font size="2">$</font></b><b><font size="2">Lock</font></b><b><font size="2">; exit&#39; 1 2 3 4 5 6 7 8 10 11 12 13 14 15<br>
echo `uname -n` $$ `date` &gt;</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 &quot;`date` - Finished synchronization&quot; &gt;&gt; </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.&nbsp; Or just have one server be the designated initiator of the rsync, which simplifies things greatly.</font><br>
</font></div><br><br>