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


_______________________________________________
Roundtable mailing list
Roundtable@muug.mb.ca
http://www.muug.mb.ca/mailman/listinfo/roundtable



--
Sean Walberg <sean@ertw.com>    http://ertw.com/