CRON

From DD-WRT Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 17:36, 5 April 2008 (edit)
AlexGhitza (Talk | contribs)
m (typo)
← Previous diff
Revision as of 19:33, 21 April 2008 (edit) (undo)
S2s2 (Talk | contribs)
m (minor draft update for clarity, structure, spelling (in progress))
Next diff →
Line 3: Line 3:
=Prerequisites= =Prerequisites=
-In order for cron to function properly, you must have the date and time set correctly.+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.
You can check this in the GUI by going to: You can check this in the GUI by going to:
* Status * Status
** Router tab ** Router tab
*** Current Time. *** Current Time.
-You can also check the time in the command line by using the 'date' command. +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 must also enable ''crontab'' in DD-WRT before you can use it.
Line 17: Line 17:
and set it to enable. and set it to enable.
-==File Locations==+=File Locations=
Cron can be set in two locations on dd-wrt: Cron can be set in two locations on dd-wrt:
Line 34: Line 34:
* * * * * root /bin/date > /tmp/test.txt * * * * * root /bin/date > /tmp/test.txt
-You must add the items you wish to CRON to the startup(broken) or firewall scripts to make them exist after the router restarts. To do this, follow the example under "Extras".+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. Examples given in the dreamhost link above or elswhere do not include "root" and so it is not apparent that you must include it.+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.
-If you do not use "root" you may have a frustrating time - your file will run from telnet or ssh (eg Putty) because you are logged on as root but will not run from crontab.+Without using "root", your test only works in telnet or ssh (eg Putty) because you already logged in as root.
-==Restarting Crond==+=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: 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:
- stopservice cron && startservice cron+Older versions of DD-WRT:
- +* stopservice cron && startservice cron
-'''Since end of June cron changed to crond!!!'''+Newer versions of DD-WRT:
 +* stopservice crond && startservice crond
==Extras== ==Extras==
Line 55: Line 56:
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). 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).
-also note: In DD-wrt v23 sp1 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 script, you will see this in your crontab file:

Revision as of 19:33, 21 April 2008

The crontab command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. Dreamhost crontab wiki has a good explanation of how crontab works. A more official explanation can be found here.

Contents

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. 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 enable crontab in the GUI by going to:

  • Administration
    • Management
      • Cron

and set it to enable.

File Locations

Cron can be set in two locations on dd-wrt:

1. Either by putting your command in the /tmp/crontab file

echo '  *      *       *       *       *       root /bin/date > /tmp/test.txt' >> /tmp/crontab

which will look like this:

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

echo '* * * * * root /bin/date > /tmp/test.txt' > /tmp/cron.d/date

which will look like this:

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

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

Extras

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:

/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 script, you will see this in your 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