TechInfoDepot:DD-WRT/CRON

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
 * 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:




 * 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:



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: 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:

$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.

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: 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.