DD-WRT:CRON/pl

Introduction
Cron, also referred to as crond, is a service which can be turned on in DD-WRT systems. Cron is the standard system scheduler process within a system running Linux and other *nix variants. Older versions of DD-WRT Linux use the name and command cron; in newer builds the name cron has been changed to crond, however they work the same.

Cron can be enabled in the Services/Services (v23) or Administration/Management (v24) section of the Web Interface of DD-WRT.

When cron starts during DD-WRT device boot, it scans for cron job instructions. These can be entered directly in the Web Interface of DD-WRT (GUI) or, for more advanced users, saved in files in certain locations (see below). Whenever cron instructions are saved as files, they are called crontab files. See the links at the bottom of this article for the formatting of cron job instructions.

Prerequisites
Because Cron executes commands according to at least the current time, the date and time should normally be set NTP on the device running DD-WRT. Having the time & date set is so important, the configuration for NTP is located directly in the first setup page under Basic/Setup Basic.

You can check the current date and time in the GUI by going to:
 * Status/Router/Current Time
 * You can also check the time in the shell interface by typing 'date'.

To make sure Cron/Crond is enabled, go to: and make sure it is set to enable.
 * Administration/Management/Cron

Cron Job Instruction: Locations
Cron job instructions can be stored in at least three locations on DD-WRT: It is important that you include "root" before the command, otherwise the command will not run as specified. The examples, in external links above or elsewhere, leave off prefixing commands with "root". External examples outside this Wiki and DD-WRT, are designed for common multi-account Linux systems: cron normally runs instructions inside crontab files as dependent on whichever user created them. Without using "root", your test only works in telnet or ssh (eg Putty) because you already logged in as root.
 * 1) Put your crontab-formatted instructions in the Web Interface cron box on the Administration->Management screen. This is the easiest method. Using stopservice cron && startservice cron in the console will store those commands in /tmp/cron.d/cron_jobs. Files stored in /tmp are cleared out at reboot time so it would be wise to preserve a copy of any needed commands, ideally using a mounted filesystem or an SD card if your router is equipped with one.
 * 2) Crontab files can be created and saved in /tmp/cron.d, which is stored in RAM and lost when restarting the DD-WRT device or loss of power. Commands added to the cron_jobs file are displayed on the Administration->Management page. As noted above, using stopservice cron && startservice cron will store those commands in /tmp/cron.d/cron_jobs.
 * 3) Crontab files can be stored in /jffs/etc/crontab, if you use the JFFS filesystem. See JFFS.

Other methods, older versions
Other methods and older versions of DD-WRT echo ' *      *       *       *       *       root /bin/date > /tmp/test.txt' >> /tmp/crontab
 * Add your command to the file /tmp/crontab. The added job instructions are erased during restart. The following command in a Startup Command in the GUI, or script file (.sh) stored in one of the locations the DD-WRT device scans during startup with the extension *.startup (See Wiki article Script Execution):

which will look like this in the /tmp/crontab file:

*     *       *       *       *       root /bin/date > /tmp/test.txt

echo '* * * * * root /bin/date > /tmp/test.txt' > /tmp/cron.d/date
 * Create a file in the /tmp/cron.d/ folder (eg. /tmp/cron.d/dosomething) using crontab formatting. A script file executed during startup, or in the Startup Command in the GUI, might look like this:

Looks like this in the dosomething file:

* * * * * root /bin/date > /tmp/test.txt

Restarting Crond
After you modify/add the appropriate command you will need to restart cron in order for your changes to take effect. You can restart crond with the following command: Older versions of DD-WRT: Newer versions of DD-WRT:
 * stopservice cron && startservice cron
 * stopservice crond && startservice crond

Note: v24sp1 deletes the /tmp/cron.d directory when the cron service is stopped. You should stop the service, make the /tmp/cron.d directory, put your cron script in that directory, then restart the cron service.

Additional Information
You can add this to your Startup Commands (Administration -> Diagnostics in the web UI), before you add any jobs. It will add clarity to your crontab file: /bin/sh -c 'echo "# minute (0-59)," >> /tmp/crontab' /bin/sh -c 'echo "# |     hour (0-23)," >> /tmp/crontab' /bin/sh -c 'echo "# |     |       day of the month (1-31)," >> /tmp/crontab' /bin/sh -c 'echo "# |     |       |       month of the year (1-12)," >> /tmp/crontab' /bin/sh -c 'echo "# |     |       |       |       day of the week (0-6 with 0=Sunday)." >> /tmp/crontab' /bin/sh -c 'echo "# |     |       |       |       |       commands" >> /tmp/crontab'

Please note: Things that execute in the startup script are invoked without a shell, so stdout and stderr pipes will not be interpreted properly if you don't invoke the shell manually (/bin/sh -c).

Note: If your device is using v23sp1, the startup script is broken, so you will want to save all this as the "firewall" script.

If you add this to your Startup Commands, you will see this in the /tmp/crontab file:

# |     hour (0-23), # |     |       day of the month (1-31), # |     |       |       month of the year (1-12), # |     |       |       |       day of the week (0-6 with 0=Sunday). # |     |       |       |       |       commands
 * 1) minute (0-59),

$PATH Differences
Cron runs with a different $PATH variable than you have in your shell when you Telnet/SSH to the router. The $PATH variable contains locations for the shell to look for applications when you try to run a them without a full path to the application. This means that Cron may not find programs you're trying to use. You can either check what the full path to apps is using the which command and then use the full path within your scripts, such as /usr/bin/uptime instead of just uptime, or you can check what your $PATH is by running echo PATH=$PATH in your shell and then copying the output to the top of your crontab or script.

root@DD-WRT:~# which uptime /usr/bin/uptime

root@DD-WRT:~# echo PATH=$PATH PATH=/bin:/usr/bin:/sbin:/usr/sbin:/jffs/sbin:/jffs/bin:/jffs/usr/sbin:/jffs/usr/bin:/mmc/sbin:/mmc/bin:/mmc/usr/sbin:/mmc/usr/bin:/opt/sbin:/opt/bin:/opt/usr/sbin:/opt/usr/bin

CRON Service falling asleep in V24
The cron service is frequently falling asleep and cron jobs are not executed. This requires a stopservice cron; startservice cron or a reboot. V23 does not have this problem.

Working Directory
Cron runs with the same working directory that you have when you Telnet/SSH to the router which is:

root@DD-WRT:~# pwd /tmp/root

If you're saving files without a path then they'll end up in this directory. Be sure your paths to files are correct so that you're not looking for them in the wrong places.