I&#39;d use a lock file just to be safe. Depending on how &quot;restart-daemon&quot; works you might end up with a race condition inside there, resulting in no daemon getting run until the next pass.<div><br></div><div>The easiest way would be</div>
<div><br></div><div>lock=&quot;/var/run/mylock&quot;</div><div>if [ -f $lock ]; then exit 1; else touch $lock; fi</div><div>#do your magic</div><div>rm $lock</div><div><br></div><div>There are other utilities like flock and lockfile if you are really concerned about re-entrancy, and depending on the user you run this script as, you may want to put $lock out of a normal user&#39;s reach. But to protect something running from cron, this will be more than enough.</div>
<div><br></div><div>Sean</div><div><br><div class="gmail_quote">On Thu, Jul 12, 2012 at 7:59 PM, Adam Thompson <span dir="ltr">&lt;<a href="mailto:athompso@dmts.biz" target="_blank">athompso@dmts.biz</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I just wrote the following snippet to quickly rotate some log files.  I know this runs without bound, that&#39;s acceptable and desirable in this case.  What I&#39;m worried about is creating race conditions if this gets run from cron every minute...<br>

<br>
    ###check if daemon has died###<br>
    if daemon-is-dead; then<br>
        L=mydaemon.log<br>
        shopt -u failglob<br>
        for i in $( ls -1r ${L}.[0-9]* 2&gt;/dev/null ); do<br>
                N=${i#$L.}<br>
                M=$(( ${N} + 1 ))<br>
                mv -n $i &quot;${L}.${M}&quot;<br>
        done<br>
        if [ -f ${L} ] ; then mv -n ${L} ${L}.1; fi<br>
        restart-daemon<br>
    fi<br>
<br>
Am I shooting myself in the foot here?  The obvious race condition is if two copies run simultaneously, but this is only for temporary debugging purposes.  If necessary, I&#39;ll change the cron job from every minute to every five minutes.  Even then, the &#39;-n&#39; option to GNU mv should protect me...?<br>

Does anyone have a safer way to do this sort of thing manually?  I don&#39;t want this logfile managed by logrotate(8).<br>
<br>
-Adam Thompson<br>
 DMTS (Contractor)<br>
 <a href="mailto:athompso@dmts.biz">athompso@dmts.biz</a><br>
 <a href="tel:%28204%29%20291-7950" value="+12042917950">(204) 291-7950</a> - direct<br>
 <a href="tel:%28204%29%20489-6515" value="+12044896515">(204) 489-6515</a> - fax<br>
<br>
<br>
_______________________________________________<br>
Roundtable mailing list<br>
<a href="mailto:Roundtable@muug.mb.ca">Roundtable@muug.mb.ca</a><br>
<a href="http://www.muug.mb.ca/mailman/listinfo/roundtable" target="_blank">http://www.muug.mb.ca/mailman/listinfo/roundtable</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Sean Walberg &lt;<a href="mailto:sean@ertw.com" target="_blank">sean@ertw.com</a>&gt;    <a href="http://ertw.com/" target="_blank">http://ertw.com/</a><br>
</div>