<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. 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.<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"><<a moz-do-not-send="true"
href="mailto:athompso@dmts.biz" target="_blank">athompso@dmts.biz</a>></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's acceptable
and desirable in this case. What I'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>/dev/null ); do<br>
N=${i#$L.}<br>
M=$(( ${N} + 1 ))<br>
mv -n $i "${L}.${M}"<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'll
change the cron job from every minute to every five minutes.
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? I don't want this logfile managed by
logrotate(8).<br>
<br>
-Adam Thompson<br>
DMTS (Contractor)<br>
<a moz-do-not-send="true" href="mailto:athompso@dmts.biz">athompso@dmts.biz</a><br>
<a moz-do-not-send="true" href="tel:%28204%29%20291-7950"
value="+12042917950">(204) 291-7950</a> - direct<br>
<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 <<a moz-do-not-send="true"
href="mailto:sean@ertw.com" target="_blank">sean@ertw.com</a>>
<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"><grdetil@scrc.umanitoba.ca></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>