As long as I can remember, Slackware has used Dillon's cron dcron as its cron daemon. Dcron is a lightweight cron with an emphasis on a solid secure codebase which, while missing a few esoteric features found in bigger cron daemons, is great for most users.
Since 13.1 and continuing with 13.37, Slackware has had an updated version of dcron that introduced some very handy features that make it especially suitable for laptops, or indeed any machine that spends much of its time in sleep mode. The features give dcron some of the characteristics of anacron.
Somehow I managed to miss the changes until now, almost two years later. (I must have been in a personal sleep mode.) Here I am going to explain the features and why they are great for laptops, and give a couple of examples.
SleepOver the years you have probably picked up several handy scripts that you run from a cron job. Some of them might run every day, some of them every week. For example, I have a weather script that grabs the local weather forecast from the Bureau of Meteorology website, parses it, and emails me a summary of today's forecast at 7:10 every morning. What if my laptop is in sleep mode at 7:10am? In that case, the script does not get a chance to run because 7:10am has lapsed by the time I open my laptop.
ID and FREQNow dcron lets you give an ID to a cron job which acts as a reference to that job. The point of having a reference is that the job no longer disappears into thin air if its activation time has passed while the machine was asleep.
The FREQ keyword specifies how often you want cron to check if the job ID has run in order to reschedule it if it has not. In the case of my weather script, the crontab entry looks like this, with the original entry first and the improved entry second (in case it is not obvious, I deleted the first entry in the real crontab):
With the second entry and ignoring the ID and FREQ keywords for the moment, the script runs every 10 past and 40 past the hour for the hours of 7, 8, 9, 10 and 11 o'clock. Obviously I do not want it to run at each of those times and this is where the FREQ keyword comes in. The FREQ entry says to run the job only if it has been at least 4 hours since it last ran, and to check on this every 30 minutes.
Take this morning as an example. I opened the laptop at 10:15am. The last time this entry ran was yesterday morning. Upon wakeup, dcron checks this entry and notices that job ID of weather last ran more than 4 hours ago, so it is good to go. At 10:40 (the next available slot), the job ran. Since then, it has not run again because before 14:40 it is less than 4 hours since it last ran, and after 14:40 it is outside its scheduled times of 7-11.
Most of my personal crontab entries now use this method.
You can see why this type of crontab entry needs an ID, because dcron needs some way of tracking how recently a particular job ran. Rather than use an internally-generated ID, it gives you the ability to specify your own. A side benefit is that the cron emails use the ID in their subject header (which is why I use meaningful words rather than a number).
SchedulingA few points on scheduling. You need to allow sufficient range in your times so that the job can run. Note the weather script previously had only 7am specified, now it has a range of 7-11 to allow a wide time period for the script to run (presumably I am not interested in getting the forecast after 11am). And, the FREQ range needs to be long enough so that the job will not run again until the next time period, but short enough so that it can run again. Confused? An example.
If I want a job to run every day, I should not specify
FREQ=1d. Say I had
a range of 7-11 hours, and the job ran at 11:40. The next day I open the laptop early
and, at 7:10am, the job would not run because it had not been 24 hours since the last
run. This is not what I want. If I specify
FREQ=12h, the job would run at
Conversely, I should not specify
FREQ=2h because then the job would run at,
for example, 7:10am, 9:10am and 11:10am if my laptop was open early, again not what I want.
The FREQ should be long enough so that the job cannot run again within the same time period, but short enough so that if it last ran at the latest time it can next run at the earliest time.
Other FeaturesThere are some other features in the new dcron too.
- FREQ can re-run a job if it did not exit cleanly (use exit 11 to signal this).
- A job can depend on another job(s) such as "run job2 only if job1 (and job0 and … ) has already run". Use the keyword AFTER.
- You can now use the keywords of @hourly, @daily, @weekly, @monthly and @yearly, which users of other crons have missed in dcron.
SummaryThe new cron daemon introduces some features that let cron jobs that are missed while a machine is in sleep mode run once the machine is wakened. This provides some of the features of anacron. It also gives extra flexibility with jobs that can depend on other jobs, and with new keywords.