cron in depth

Tim Pinkawa

Revision History
Revision 1.02009-02-03tim
Initial document converted to DocBook.

Table of Contents

Introduction
DD-WRT cron
cron Startup Process
cron Caveats
Summary
To store a persistent crontab...
For temporary crontabs and experimenting...
Alternatives

Introduction

cron is a classic Unix daemon for scheduling commands to be executed at a given frequency. A brief history of cron can be found here.

DD-WRT cron

DD-WRT's cron is based on Paul Vixie's cron implementation. It can be enabled by checking Enable in the Administration tab of the DD-WRT web interface or by issuing the command nvram set cron_enable 1.

cron Startup Process

Before the cron daemon is started, the start_cron function is called to prepare the environment. It creates the /var/spool/ and /var/spool/cron/ directories if they don't already exist, then creates /tmp/cron.d to store cron jobs. On traditional Unix/Linux systems, cron.d is stored in /etc and rather than modifying paths in the cron source code, /etc/cron.d is symlinked to /tmp/cron.d on DD-WRT. It is important to note that while the /var/ folders are persistent across cron processes, cron.d is deleted and recreated not only after reboots, but every time cron is started or stopped using startservice and stopservice. By default, a cron job called check_ps is added which runs every two minutes and monitors daemon activity. start_cron will also read the cron_jobs NVRAM setting which is a newline separated list of crontab style entries. When start_cron runs, it reads the lines from cron_jobs and writes them to /tmp/cron.d/cron_jobs. As of DD-WRT v24 SP1, start_cron attempts to copy cron.d style files from /tmp/mycron.d/, /jffs/mycron.d/, and /mmc/mycron.d/ to /tmp/cron.d/, but a bug in the code prevents it. cron also will read /tmp/crontab when it starts, however, unlike /tmp/cron.d/, /tmp/crontab is persistent across cron restarts (NOT system restarts), so it is possible to stop cron, change /tmp/crontab, and restart cron.

cron Caveats

cron reads the contents of /tmp/cron.d/* and /tmp/crontab once and only once when cron starts. If you modify the files, delete files, or add files, they will not be read until cron is restarted and the files exist at load time. This has an irritating consequence when working with /tmp/cron.d/. Anything added or changed in /tmp/cron.d/ will not take effect when added. When cron is stopped with stopservice, /tmp/cron.d/ is removed and when started again, it will only contain check_ps. With the mycron.d functionality not working in v24 SP1, the best way to have a persistant crontab that is restored on boot is to use the cron_jobs NVRAM setting. The web interface provides a textbox to add lines of crontab style entries in the Administration tab.

Summary

To store a persistent crontab...

Use the cron_jobs NVRAM option.

For temporary crontabs and experimenting...

Stop cron, edit /tmp/crontab, start cron.

Alternatives

To avoid a potentially massive crontab list stored in NVRAM, the following shell script can be used to provide equivalent functionality to using /mmc/etc/cron.d, /jffs/etc/cron.d, and /tmp/mycron.d to store persistent crontabs.

Example 1. /jffs/etc/config/cron.startup

#!/bin/sh
stopservice cron
mkdir /tmp/cron.d
echo "*/2 * * * * root /sbin/check_ps" > /tmp/cron.d/start_ps
cp -af /tmp/mycron.d/* /tmp/cron.d/
cp -af /mmc/etc/cron.d/* /tmp/cron.d/
cp -af /jffs/etc/cron.d/* /tmp/cron.d/
cron

This script should also be used in place of startservice cron when starting cron. stopservice cron should still be used to stop cron.