<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Allen Brooker (AllenJB) &#187; script</title>
	<atom:link href="http://allenjb.me.uk/?feed=rss2&#038;tag=script" rel="self" type="application/rss+xml" />
	<link>http://allenjb.me.uk</link>
	<description>Programming yesterday was about developers vs idiots. Now the developers are the idiots.</description>
	<lastBuildDate>Tue, 31 Aug 2010 12:45:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cron Scripts on Linux</title>
		<link>http://allenjb.me.uk/?p=172</link>
		<comments>http://allenjb.me.uk/?p=172#comments</comments>
		<pubDate>Wed, 28 Oct 2009 23:09:51 +0000</pubDate>
		<dc:creator>AllenJB</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://allenjb.me.uk/?p=172</guid>
		<description><![CDATA[OK. So this week I&#8217;ve been banging my head against the previously mentioned 5 year old bug in run-crons.
The problem is, for /etc/cron.{hourly,daily,weekly,monthly} we have the following requirements:
1) Scripts should ideally be run at a specified time.
2) Ideally the time at which the scripts run should be slightly different for each of the groupings to [...]]]></description>
			<content:encoded><![CDATA[<p>OK. So this week I&#8217;ve been banging my head against the previously mentioned 5 year old bug in run-crons.</p>
<p>The problem is, for /etc/cron.{hourly,daily,weekly,monthly} we have the following requirements:<br />
1) Scripts should ideally be run at a specified time.<br />
2) Ideally the time at which the scripts run should be slightly different for each of the groupings to avoid unnecessarily loading the system and reduce potential conflicts in related scripts in different groups.<br />
3) Scripts should never run twice within the same time period<br />
4) If the scripts are not run for any reason in their given time period (eg. the system is offline), we want to run them at the next convenient opportunity<br />
5) Even if the scripts are not run at their normal time for any reason, we want to run them at the normal time in the next time period<br />
6) Because this is something very useful, we want to do this with as few resources or dependencies as possible on as many platforms as possible</p>
<p>So the solution the world came up with is run-crons. Which works. Most of the time.</p>
<p>The real headache comes because of daylight savings. Even if we don&#8217;t schedule to run in daylight savings, it leaves its mark in the form of an extra or less hour in the day that&#8217;s unexpected. This causes a problem because run-crons compares the timestamp on its &#8220;last run&#8221; spool file with the current time. Once this gets more than 24 hours old, we want to run the daily scripts again&#8230; except on daylight savings changes, when this figure is 23 or 25 hours. And we&#8217;re trying to do this in a shell script, using the most generic tools possible.</p>
<p>So we could use &#8220;3am&#8221; as the time to compare to &#8211; but then run-crons has to know this time. It also still brings up the issue of if the system was down for 20 hours, and comes back up at 11pm, do you want to run the cron.daily scripts for that day still? Then again at 3am the following day?</p>
<p>Brain damage ensues.</p>
<p>Frankly I&#8217;m beginning to think the world would be a better place if we did away with cron.{hourly,daily,weekly,monthly} altogether and just used cron.d. But even that still doesn&#8217;t resolve all the issues.</p>
]]></content:encoded>
			<wfw:commentRss>http://allenjb.me.uk/?feed=rss2&amp;p=172</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Attack of the 5 year old DST / run-crons bug!</title>
		<link>http://allenjb.me.uk/?p=167</link>
		<comments>http://allenjb.me.uk/?p=167#comments</comments>
		<pubDate>Tue, 27 Oct 2009 23:42:39 +0000</pubDate>
		<dc:creator>AllenJB</dc:creator>
				<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://allenjb.me.uk/?p=167</guid>
		<description><![CDATA[So I noticed that when we switched off the abomination that is daylight savings this weekend just gone, my systems which run on &#8220;GB&#8221; time rather than &#8220;UTC&#8221; executed their cron scripts twice. This despite the fact that, at a glance, they should execute at (or shortly after) 03:01, which should be safe.
Turns out this [...]]]></description>
			<content:encoded><![CDATA[<p>So I noticed that when we switched off the abomination that is daylight savings this weekend just gone, my systems which run on &#8220;GB&#8221; time rather than &#8220;UTC&#8221; executed their cron scripts twice. This despite the fact that, at a glance, they should execute at (or shortly after) 03:01, which should be safe.</p>
<p>Turns out this isn&#8217;t quite the case. There&#8217;s a <a href="http://bugs.gentoo.org/show_bug.cgi?id=69777">5 year old bug in Gentoo&#8217;s Bugzilla</a> about this known issue and it appears to be down to the way run-crons manages its lock / last run files. I&#8217;m already running a run-crons <a href="http://bugs.gentoo.org/show_bug.cgi?id=261000">patched for recursive directory scanning</a> on some of my systems, so it wasn&#8217;t that big a step to basically do away with Gentoo&#8217;s supplied script altogether.</p>
<p>Now I&#8217;m running &#8220;run-crons-in&#8221; &#8211; a much simplified script that simply relies on the cron daemon itself for timing (which is how it should be, in my opinion). Here&#8217;s my script:</p>
<pre>
#!/bin/bash
# 2009-10-27 Complete rewrite of run-crons
# This version is designed to be called using @hourly @daily @weekly or @monthly
# With, for example: run-crons-in daily
#
# This version assumes:
# - We don't care if a previous script is still running
# - The cron daemon handles all timing in a sensible manner
# - Therefore no locking is necessary

AJB_DEBUG=0
AJB_TIMES=1
AJB_PTIMES=1
AJB_TIMEFORMAT='%Y-%m-%d %T'
BASE=$1

AJB_STARTTIME="`date +\"${AJB_TIMEFORMAT}\"`"
[ $AJB_TIMES -eq 1 ] &#038;&#038; echo "Start time: ${AJB_STARTTIME}"

run_recursive() {
        CRONDIR="$1"
        [ $AJB_DEBUG -eq 1 ] &#038;&#038; echo "Executing cron scripts in directory: $CRONDIR"
        for SCRIPT in $CRONDIR/* ; do
                if [[ -x $SCRIPT &#038;&#038; ! -d $SCRIPT ]]; then
                        [ $AJB_DEBUG -eq 1 ] &#038;&#038; echo "Executing cron script: $SCRIPT"
                        AJB_PSTARTTIME="`date +\"${AJB_TIMEFORMAT}\"`"
                        [ $AJB_PTIMES -eq 1 ] &#038;&#038; echo "Script start time: ${AJB_PSTARTTIME}"
                        $SCRIPT
                        AJB_PSTOPTIME="`date +\"${AJB_TIMEFORMAT}\"`"
                        [ $AJB_PTIMES -eq 1 ] &#038;&#038; echo "Script stop time: ${AJB_PSTOPTIME}"
                fi

                if [[ -d $SCRIPT ]]; then
                        run_recursive $SCRIPT
                fi
        done
}

CRONDIR="/etc/cron.${BASE}"

test -d "$CRONDIR" || echo "Directory does not exist or is not a directory: ${CRONDIR}"
set +e
test -d "$CRONDIR" &#038;&#038; run_recursive "$CRONDIR"

AJB_STOPTIME="`date +\"${AJB_TIMEFORMAT}\"`"
[ $AJB_TIMES -eq 1 ] &#038;&#038; echo "End time: ${AJB_STOPTIME}"
</pre>
<p>As you can see, it&#8217;s fairly basic &#8211; altho I&#8217;ve added some extra output for timing and debugging, simply because I felt like it. I&#8217;m currently testing this long term to ensure it works as expected, but hopefully I&#8217;ll adopt this for all my systems within a couple of months.</p>
<p>The full /etc/crontab entries are now:</p>
<pre>
@hourly   root    /usr/local/sbin/run-crons-in hourly
@daily    root    /usr/local/sbin/run-crons-in daily
@weekly   root    /usr/local/sbin/run-crons-in weekly
@monthly  root    /usr/local/sbin/run-crons-in monthly
</pre>
<p>(Yes, I realize that, at least by the man page, those are all going to run at midnight, but daily is the only set of scripts that ever does anything really stressful on the system I&#8217;m currently testing on, and it&#8217;s a quad core, and I really don&#8217;t care otherwise I wouldn&#8217;t be testing cron daemons on it)</p>
<p>I&#8217;m also taking the opportunity to use <a href="https://fedorahosted.org/cronie/">cronie</a> in place of vixie-cron. There&#8217;s little documentation for this project, but as far as I can see it&#8217;s a drop-in replacement for / fork of vixie-cron from redhat that&#8217;s actively maintained.</p>
<p>I did take a quick look at the other cron daemons in the portage tree, but cronie appears to be the only one that&#8217;s actively maintained.</p>
]]></content:encoded>
			<wfw:commentRss>http://allenjb.me.uk/?feed=rss2&amp;p=167</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
