CRON/zh cn

From DD-WRT Wiki

Jump to: navigation, search

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

当cron在DD-WRT设备开机启动时,它会扫描cron作业指令。这些可以直接在DD-WRT的(GUI)的网络接口输入,或者更高级的用户,在某些位置的文件保存(见下文)。每当cron的指示作为文件保存起来,他们被称为的crontab 文件。看到在这个cron作业指令的格式文章底部的链接。


[edit] 先决条件



  • 【状态】->【路由器信息】下的【当前时间】(English:Status/Router/Current Time)
  • 您还可以【管理】->【命令】->【指令解释器】(即shell)界面中输入'date'命令。

确定Cron/Crond 为启用状态,参:

  • 【管理】->【管理】下的【Cron】项(English:Administration/Management/Cron)


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

[edit] 其他方法,旧版本

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

[edit] 附加信息

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

[edit] 外部链接