CRON/zh cn

From DD-WRT Wiki

(Difference between revisions)
Jump to: navigation, search

Leangjia (Talk | contribs)
(New page: {{Languages|CRON}} '''Cron''', 也被称为'''crond''', 这是一个可以在DD-WRT的系统上打开的服务。打开步骤为【管理】->【管理】->【Cron】->选中【启用】...)
Next diff →

Revision as of 15:26, 4 August 2010

Cron, 也被称为crond, 这是一个可以在DD-WRT的系统上打开的服务。打开步骤为【管理】->【管理】->【Cron】->选中【启用】即可。 cron是Linux和其他的* nix varients系统标准的系统内运行的调度进程。DD-WRT的旧版本的Linux使用的名称和命令的cron,在较新版本的名称已被更改为玉米crond,但他们的工作是相同的。 Cron可以启用的服务/服务(v23)或行政/管理(v24)的DD - WRT的Web界面部分。

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.

Contents

先决条件

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.

Cron工作指引:位置

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.
  2. Crontab files can be created and saved in /tmp/cron.d, which is in RAM and lost when restarting the DD-WRT device, or loss of power.
  3. Crontab files can be stored in /jffs/etc/crontab. See JFFS.
  4. When using backslash or single quote in the crontab file via GUI, be aware that V24 has a bug (#3872) 27-Mar-2010

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

重新启动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

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.

附加信息

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

外部链接