<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Agree about the lock file to prevent race conditions.<br>
    <br>
    I think you'll also run into problems when M reaches 10, because ls
    -1r won't sort numerically when the number of digits varies.&nbsp; You'd
    need to either stick to to single digits for the version number, or
    pipe the ls output to "sort -t. -k 3nr" to get a proper reverse
    numeric sort.&nbsp; Otherwise the rotation order will get really messed
    up.<br>
    <br>
    <div class="moz-cite-prefix">On 12/07/2012 9:04 PM, Sean Walberg
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAMi1z0DvLK5vdiR7qxHp1t6pfLHjpUtMjrv2iS81E7k21wgPOA@mail.gmail.com"
      type="cite">I'd use a lock file just to be safe. Depending on how
      "restart-daemon" 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="/var/run/mylock"</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'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 moz-do-not-send="true"
              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. &nbsp;I know this runs without bound, that's acceptable
            and desirable in this case. &nbsp;What I'm worried about is
            creating race conditions if this gets run from cron every
            minute...<br>
            <br>
            &nbsp; &nbsp; ###check if daemon has died###<br>
            &nbsp; &nbsp; if daemon-is-dead; then<br>
            &nbsp; &nbsp; &nbsp; &nbsp; L=mydaemon.log<br>
            &nbsp; &nbsp; &nbsp; &nbsp; shopt -u failglob<br>
            &nbsp; &nbsp; &nbsp; &nbsp; for i in $( ls -1r ${L}.[0-9]* 2&gt;/dev/null ); do<br>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; N=${i#$L.}<br>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; M=$(( ${N} + 1 ))<br>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mv -n $i "${L}.${M}"<br>
            &nbsp; &nbsp; &nbsp; &nbsp; done<br>
            &nbsp; &nbsp; &nbsp; &nbsp; if [ -f ${L} ] ; then mv -n ${L} ${L}.1; fi<br>
            &nbsp; &nbsp; &nbsp; &nbsp; restart-daemon<br>
            &nbsp; &nbsp; fi<br>
            <br>
            Am I shooting myself in the foot here? &nbsp;The obvious race
            condition is if two copies run simultaneously, but this is
            only for temporary debugging purposes. &nbsp;If necessary, I'll
            change the cron job from every minute to every five minutes.
            &nbsp;Even then, the '-n' option to GNU mv should protect me...?<br>
            Does anyone have a safer way to do this sort of thing
            manually? &nbsp;I don't want this logfile managed by
            logrotate(8).<br>
            <br>
            -Adam Thompson<br>
            &nbsp;DMTS (Contractor)<br>
            &nbsp;<a moz-do-not-send="true" href="mailto:athompso@dmts.biz">athompso@dmts.biz</a><br>
            &nbsp;<a moz-do-not-send="true" href="tel:%28204%29%20291-7950"
              value="+12042917950">(204) 291-7950</a> - direct<br>
            &nbsp;<a moz-do-not-send="true" href="tel:%28204%29%20489-6515"
              value="+12044896515">(204) 489-6515</a> - fax<br>
            <br>
            <br>
            _______________________________________________<br>
            Roundtable mailing list<br>
            <a moz-do-not-send="true"
              href="mailto:Roundtable@muug.mb.ca">Roundtable@muug.mb.ca</a><br>
            <a moz-do-not-send="true"
              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 moz-do-not-send="true"
          href="mailto:sean@ertw.com" target="_blank">sean@ertw.com</a>&gt;&nbsp;
        &nbsp; <a moz-do-not-send="true" href="http://ertw.com/"
          target="_blank">http://ertw.com/</a><br>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Roundtable mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Roundtable@muug.mb.ca">Roundtable@muug.mb.ca</a>
<a class="moz-txt-link-freetext" href="http://www.muug.mb.ca/mailman/listinfo/roundtable">http://www.muug.mb.ca/mailman/listinfo/roundtable</a>
</pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Gilles R. Detillieux              E-mail: <a class="moz-txt-link-rfc2396E" href="mailto:grdetil@scrc.umanitoba.ca">&lt;grdetil@scrc.umanitoba.ca&gt;</a>
Spinal Cord Research Centre       WWW:    <a class="moz-txt-link-freetext" href="http://www.scrc.umanitoba.ca/">http://www.scrc.umanitoba.ca/</a>
Dept. Physiology, U. of Manitoba  Winnipeg, MB  R3E 0J9  (Canada)
</pre>
    <br>
    <br>
  </body>
</html>