CRON

From DD-WRT Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 19:33, 21 April 2008 (edit)
S2s2 (Talk | contribs)
m (minor draft update for clarity, structure, spelling (in progress))
← Previous diff
Revision as of 20:15, 21 April 2008 (edit) (undo)
S2s2 (Talk | contribs)
m ([continued edit] Original articles' clarity and structure was horrible. Revising.)
Next diff →
Line 1: Line 1:
-The ''crontab'' command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. [http://wiki.dreamhost.com/index.php/Crontab Dreamhost crontab wiki] has a good explanation of how crontab works. A more official explanation can be found [http://en.wikipedia.org/wiki/Crontab here].+'''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 varients. 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 saved in files in certain locations. When the instructions are saved as files for Cron, they are called '''crontab''' files. See the links at the bottom of this article for the formatting of cron job instructions.
=Prerequisites= =Prerequisites=
-In order for crond & crond preference files, crontabs, to function properly, you must have the date and time set correctly on the device running DD-WRT.+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 this in the GUI by going to:+
-* Status+
-** Router tab+
-*** Current Time.+
-You can also check the time in the shell interface by typing 'date'. +
-You must also enable ''crontab'' in DD-WRT before you can use it.+You can check the current date and time in the GUI by going to:
-You can enable crontab in the GUI by going to:+* Status/Router/Current Time
-* Administration+* You can also check the time in the shell interface by typing 'date'.
-** Management+ 
-*** Cron+To make sure Cron/Crond is enabled, go to:
-and set it to enable.+* Administration/Management/Cron
 +and make sure it is set to enable.
=File Locations= =File Locations=
-Cron can be set in two locations on dd-wrt:+Cron job instructions can be stored in at least three locations on DD-WRT:
-1. Either by putting your command in the /tmp/crontab file+1. Put your crontab-formatted instructions in the Web Interface cron box.
 +This is the easiest method and recommended by this article author.
 + 
 +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]]):
echo ' * * * * * root /bin/date > /tmp/test.txt' >> /tmp/crontab echo ' * * * * * root /bin/date > /tmp/test.txt' >> /tmp/crontab
-which will look like this:+which will look like this in the /tmp/crontab file:
* * * * * root /bin/date > /tmp/test.txt * * * * * root /bin/date > /tmp/test.txt
-2. Or by putting a file in the /tmp/cron.d/ folder (eg. ''/tmp/cron.d/dosomething'') that holds your command+#. 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:
echo '* * * * * root /bin/date > /tmp/test.txt' > /tmp/cron.d/date echo '* * * * * root /bin/date > /tmp/test.txt' > /tmp/cron.d/date
-which will look like this:+Looks like this in the ''dosomething'' file:
* * * * * root /bin/date > /tmp/test.txt * * * * * root /bin/date > /tmp/test.txt
-You can add the items you wish to CRONd using the DD-WRT Web Interface Startup Commands or Firewall Commands, to make them exist after the router restarts. To do this, follow the example under "Extras".+'''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.
- +
-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: crontab files normally run as whichever user created them.+
Without using "root", your test only works in telnet or ssh (eg Putty) because you already logged in as root. Without using "root", your test only works in telnet or ssh (eg Putty) because you already logged in as root.
Line 46: Line 48:
* stopservice crond && startservice crond * stopservice crond && startservice crond
-==Extras==+==Additional Information==
-You may wish to add this to your startup script (Administration -> Diagnostics in the web UI), before any jobs are added. It will add clarity to your crontab file:+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 "# minute (0-59)," >> /tmp/crontab'
/bin/sh -c 'echo "# | hour (0-23)," >> /tmp/crontab' /bin/sh -c 'echo "# | hour (0-23)," >> /tmp/crontab'
Line 59: Line 61:
Note: If your device is using v23sp1, the startup script is broken, so you will want to save all this as the "firewall" script. 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 script, you will see this in your crontab file:+If you add this to your Startup Commands, you will see this in the /tmp/crontab file:
# minute (0-59), # minute (0-59),
Line 69: Line 71:
=External Links= =External Links=
 +* [https://www.dd-wrt.com/wiki/index.php?title=Special:Search&search=Cron&fulltext=Search Articles containing 'CRON' on the DD-WRT Wiki]
 +* [http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5 Unix Man Pages: Cron]
* [http://www.math-linux.com/spip.php?article45 Crontab : Scheduling Tasks] * [http://www.math-linux.com/spip.php?article45 Crontab : Scheduling Tasks]
 +* [http://wiki.dreamhost.com/index.php/Crontab Dreamhost crontab wiki]
* [http://blog.eukhost.com/2006/06/07/cron-job-in-cpanel Cron Job in cPanel] * [http://blog.eukhost.com/2006/06/07/cron-job-in-cpanel Cron Job in cPanel]
* [http://blog.webhosting.uk.com/2006/06/07/cron-jobs-automated-processes/ Cron Jobs (Automated Processes)] * [http://blog.webhosting.uk.com/2006/06/07/cron-jobs-automated-processes/ Cron Jobs (Automated Processes)]
* [http://www.bodhost.com/web-hosting/index.php/2006/11/28/cron-explanation/ Crontab Explained] * [http://www.bodhost.com/web-hosting/index.php/2006/11/28/cron-explanation/ Crontab Explained]
 +* [http://en.wikipedia.org/wiki/Crontab Wikipedia Crontab]
[[Category:English documentation]] [[Category:English documentation]]

Revision as of 20:15, 21 April 2008

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 varients. 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 saved in files in certain locations. When the instructions are saved as files for Cron, they are called crontab files. See the links at the bottom of this article for the formatting of cron job instructions.

Contents

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:

  • Administration/Management/Cron

and make sure it is set to enable.

File Locations

Cron job instructions can be stored in at least three locations on DD-WRT:

1. Put your crontab-formatted instructions in the Web Interface cron box. This is the easiest method and recommended by this article author.

Other methods and older versions of DD-WRT

  1. . 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):
echo '  *      *       *       *       *       root /bin/date > /tmp/test.txt' >> /tmp/crontab

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

  *      *       *       *       *       root /bin/date > /tmp/test.txt
  1. . 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:
echo '* * * * * root /bin/date > /tmp/test.txt' > /tmp/cron.d/date

Looks like this in the dosomething file:

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

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.

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:

  • stopservice cron && startservice cron

Newer versions of DD-WRT:

  • stopservice crond && startservice crond

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

# minute (0-59),
# |      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

External Links