[RndTbl] race conditions?

Adam Thompson athompso at dmts.biz
Thu Jul 12 19:59:50 CDT 2012


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
 (204) 291-7950 - direct
 (204) 489-6515 - fax




More information about the Roundtable mailing list