CRON

From DD-WRT Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 07:33, 20 March 2008 (edit)
Eowyn (Talk | contribs)

← Previous diff
Current revision (17:49, 18 April 2022) (edit) (undo)
Kernel-panic69 (Talk | contribs)
(Restarting Crond - (fix confusion about requirement to restart cron/crond))
 
(19 intermediate revisions not shown.)
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 explanaton can be found [http://en.wikipedia.org/wiki/Crontab here].+{{Languages|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= =Prerequisites=
-In order for cron to function properly, you must have the date and time set correctly.+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 command line by using the 'date' command. +
-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+
-and set it to enable.+
-==File Locations==+To make sure Cron/Crond is enabled, go to:
-Cron can be set in two locations on dd-wrt:+* Administration/Management/Cron
 +and make sure it is set to enable.
-1. Either by putting your command in the /tmp/crontab file+=Cron Job Instruction: Locations=
 +Cron job instructions can be stored in at least three locations on DD-WRT:
 +# 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.
 +# 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.
 +# Crontab files can be stored in /jffs/etc/crontab, if you use the JFFS filesystem. See [[JFFS]].
 +'''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.
 + 
 +==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]]):
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 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".+=Restarting Crond=
 +'''NOTE:''' v24sp1 deletes the /tmp/cron.d directory when the cron service is stopped. Should you stop the service, make the /tmp/cron.d directory, put your cron script in that directory, then restart the cron service.<br>
 +'''NOTE: For builds 41174 and older,''' after you modify/add the appropriate command you will need to restart cron in order for your changes to take effect. '''For builds 41212 and newer, a restart is not required.'''<br>
 +You can restart crond with the following commands:<br>
 +
 +*Older versions of DD-WRT:
 +<pre>stopservice cron && startservice cron</pre>
 +*Newer versions of DD-WRT:
 +<pre>stopservice crond && startservice crond</pre>
-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.+==Additional Information==
-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.+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:
- +
-==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: +
- stopservice cron && startservice cron+
- +
-'''Since end of June cron changed to 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 "# minute (0-59)," >> /tmp/crontab'
/bin/sh -c 'echo "# | hour (0-23)," >> /tmp/crontab' /bin/sh -c 'echo "# | hour (0-23)," >> /tmp/crontab'
Line 54: Line 63:
/bin/sh -c 'echo "# | | | | | commands" >> /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).+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).
-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 Commands, you will see this in the /tmp/crontab file:
# minute (0-59), # minute (0-59),
Line 67: Line 76:
# | | | | | commands # | | | | | commands
-=External Links=+=Common Problems=
-* [http://www.math-linux.com/spip.php?article45 Crontab : Scheduling Tasks]+==$PATH Differences==
-* [http://blog.eukhost.com/2006/06/07/cron-job-in-cpanel Cron Job in cPanel]+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.
-* [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]+
-[[Category:English documentation]]+ 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.
 +
 +fixed in build r41189, as per this thread: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=321629
 +
 +==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.
 +
 +
 +=External Links=
 +* [https://man7.org/linux/man-pages/man8/cron.8.html cron(8) — Linux manual page]
 +* [https://man7.org/linux/man-pages/man5/crontab.5.html crontab(5) — Linux manual page]
 +* [https://math-linux.com/linux/tip-of-the-day/article/crontab-scheduling-tasks Crontab : Scheduling Tasks]
 +* [http://en.wikipedia.org/wiki/Crontab Wikipedia Crontab]
- ==友情链接== +[[Category:Middleware settings]][[Category:Advanced tutorials]]
-[http://www.xinhaianbz.com.cn 发票]+
-[http://www.phts.cn 发电机租赁]+
-[http://www.fapiao163.com.cn 代开发票]+
-[http://www.whstwl.com 北京物流]+
-[http://www.jctdglass.com 烤漆玻璃]+
-[http://www.bjjds.cn 热水器维修]+
-[http://www.bjjds.cn 燃气灶维修]+
-[http://www.bjjds.cn 万家乐热水器维修]+
-[http://www.bj-hz.com 北京装修公司]+
-[http://www.bj-hz.com 北京装饰公司]+
-[http://www.hzst.net.cn Dell服务器]+
-[http://www.bjdaiban.com 注册北京公司]+
-[http://www.mmxxy.com 热转印机]+
-[http://www.mmxxy.com 高温瓷像]+
-[http://www.hant.cn 不锈钢字]+
-[http://www.bjsflt.cn 北京搬家]+
-[http://www.hzst.net.cn IBM服务器]+
-[http://www.hzst.net.cn hp服务器]+
-[http://www.hzst.net.cn Dell服务器] +
-[http://www.szjlit.com/ibm.html IBM服务器]+
-[http://www.szjlit.com hp服务器]+
-[http://www.szjlit.com CISCO交换机]+

Current revision


Contents

[edit] 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.

[edit] 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.

[edit] Cron Job Instruction: 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 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.

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.

[edit] 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):
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
  • 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

[edit] Restarting Crond

NOTE: v24sp1 deletes the /tmp/cron.d directory when the cron service is stopped. Should you stop the service, make the /tmp/cron.d directory, put your cron script in that directory, then restart the cron service.
NOTE: For builds 41174 and older, after you modify/add the appropriate command you will need to restart cron in order for your changes to take effect. For builds 41212 and newer, a restart is not required.
You can restart crond with the following commands:

  • Older versions of DD-WRT:
stopservice cron && startservice cron
  • Newer versions of DD-WRT:
stopservice crond && startservice crond

[edit] 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:

# 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

[edit] Common Problems

[edit] $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

[edit] 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.

fixed in build r41189, as per this thread: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=321629

[edit] 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.


[edit] External Links