[RndTbl] race conditions?

Gilles Detillieux grdetil at scrc.umanitoba.ca
Thu Jul 12 21:48:59 CDT 2012


Agree about the lock file to prevent race conditions.

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.  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.  
Otherwise the rotation order will get really messed up.

On 12/07/2012 9:04 PM, Sean Walberg wrote:
> 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.
>
> The easiest way would be
>
> lock="/var/run/mylock"
> if [ -f $lock ]; then exit 1; else touch $lock; fi
> #do your magic
> rm $lock
>
> 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.
>
> Sean
>
> On Thu, Jul 12, 2012 at 7:59 PM, Adam Thompson <athompso at dmts.biz 
> <mailto:athompso at dmts.biz>> wrote:
>
>     I just wrote the following snippet to quickly rotate some log
>     files.  I know this runs without bound, that's acceptable and
>     desirable in this case.  What I'm worried about is creating race
>     conditions if this gets run from cron every minute...
>
>         ###check if daemon has died###
>         if daemon-is-dead; then
>             L=mydaemon.log
>             shopt -u failglob
>             for i in $( ls -1r ${L}.[0-9]* 2>/dev/null ); do
>                     N=${i#$L.}
>                     M=$(( ${N} + 1 ))
>                     mv -n $i "${L}.${M}"
>             done
>             if [ -f ${L} ] ; then mv -n ${L} ${L}.1; fi
>             restart-daemon
>         fi
>
>     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'll change the cron
>     job from every minute to every five minutes.  Even then, the '-n'
>     option to GNU mv should protect me...?
>     Does anyone have a safer way to do this sort of thing manually?  I
>     don't want this logfile managed by logrotate(8).
>
>     -Adam Thompson
>      DMTS (Contractor)
>     athompso at dmts.biz <mailto:athompso at dmts.biz>
>     (204) 291-7950 <tel:%28204%29%20291-7950> - direct
>     (204) 489-6515 <tel:%28204%29%20489-6515> - fax
>
>
>     _______________________________________________
>     Roundtable mailing list
>     Roundtable at muug.mb.ca <mailto:Roundtable at muug.mb.ca>
>     http://www.muug.mb.ca/mailman/listinfo/roundtable
>
>
>
>
> -- 
> Sean Walberg <sean at ertw.com <mailto:sean at ertw.com>> http://ertw.com/
>
>
> _______________________________________________
> Roundtable mailing list
> Roundtable at muug.mb.ca
> http://www.muug.mb.ca/mailman/listinfo/roundtable

-- 
Gilles R. Detillieux              E-mail: <grdetil at scrc.umanitoba.ca>
Spinal Cord Research Centre       WWW:    http://www.scrc.umanitoba.ca/
Dept. Physiology, U. of Manitoba  Winnipeg, MB  R3E 0J9  (Canada)



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.muug.mb.ca/pipermail/roundtable/attachments/20120712/d8d9211d/attachment-0001.html>


More information about the Roundtable mailing list