Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
zh:docs:guide-user:base-system:cron [2019/11/03 12:41] – [添加和编辑cron定时任务] longgenxingzh:docs:guide-user:base-system:cron [2019/11/04 01:03] – [定期重启] longgenxing
Line 1: Line 1:
-FIXME **This page is not fully translated, yet. Please help completing the translation.**\\ //(remove this paragraph once the translation is finished)// 
- 
 ====== Cron and crontab ====== ====== Cron and crontab ======
 ====== Cron软件 及其 crontab命令使用 ====== ====== Cron软件 及其 crontab命令使用 ======
Line 39: Line 37:
  
 ===== Task specification ===== ===== Task specification =====
 +===== 设定定时任务 =====
 Each line is a separate task written in the specification: Each line is a separate task written in the specification:
 +
 +**中文翻译**:每一个任务的配置占一行,格式如下:
 +
 <code> <code>
 * * * * * command to execute * * * * * command to execute
Line 49: Line 51:
 | ----------- Hour (0 - 23) | ----------- Hour (0 - 23)
 ------------- Minute (0 - 59) ------------- Minute (0 - 59)
 +</code>
 +
 +**中文翻译**:
 +
 +<code>
 +* * * * * 需要执行的命令
 +- - - - -
 +| | | | |
 +| | | | ----- 一星期中的第几天 (0 - 6) (其中0表示星期日)
 +| | | ------- 月份 (1 - 12)
 +| | --------- 一个月中的第几天 (1 - 31)
 +| ----------- 一天中的第几小时 (0 - 23)
 +------------- 一小时中的第几分钟 (0 - 59)
 </code> </code>
  
 Examples of time specification: Examples of time specification:
 +
 +**中文翻译**:以下是指定时间的示例:
    
-^  min\\ 0-59  ^  hour\\ 0-23  ^  day/month\\ 1-31  ^  month\\ 1-12  ^  day/week\\ 0-6 ^ Description +^  分钟\\ 0-59  ^  小时\\ 0-23  ^  月中的第几天\\ 1-31  ^  月份\\ 1-12  ^  星期中的第几天\\ 0-6 ^ 执行效果 
-|   */     *        *    |      |     Every minutes +|   */     *        *    |      |     5分钟执行一次 
-|   12    |   */ |      *    |      |     Every hours at 12 minutes +|   12    |   */ |      *    |      |     每过3个小时后的第12分钟执行一次 
-|   57    |   11       15    | 1,6,12 |     At 11:57 Hrs  on 15th of Jan, June & Dec +|   57    |   11       15    | 1,6,12 |     在1、6、12月中的15豪,当天的1157分各执行一次 
-|   25    |    6        *    |    *    1-5  | At 6:25 AM every weekday (Mon-Fri) +|   25    |    6        *    |    *    1-5  | 工作日期间(周1到周5),每天早上625分执行一次 
-|          0    4,12,26  |    *       At midnight on 4th, 12th and 26th of every month +|          0    4,12,26  |    *       每月的第4、12、26日,晚上12点执行一次 
-|  5,10    9,14  |    10        *    0,4  | At 9:05AM, 9:10AM, 2:05PM and 2:10PM every Sunday and Thursday |+|  5,10    9,14  |    10        *    0,4  | 每周日、周四的早上9:05早上9:10下午2:05 以及下午2:10各执行一次|
  
 :!: 0 (zero) is treated as Sunday. :!: 0 (zero) is treated as Sunday.
 If you set the day of the week to 7, busybox will go bonkers and run your command every day. If you set the day of the week to 7, busybox will go bonkers and run your command every day.
 +
 +**中文翻译**::!: 再次提醒0就是星期天。
 +如果你将要设置的星期几,设置为7(也就是说超出0-6的范围),busybox就立即秀逗了,它会每天都执行这个定时任务。
  
 See also: See also:
 +
 +**中文翻译**:其他参考如下:
 +
   * [[man>crontab(1)]], [[man>crontab(5)]]   * [[man>crontab(1)]], [[man>crontab(5)]]
   * [[https://busybox.net/downloads/BusyBox.html#crontab|BusyBox crontab]]   * [[https://busybox.net/downloads/BusyBox.html#crontab|BusyBox crontab]]
Line 70: Line 93:
  
 ===== Logging ===== ===== Logging =====
 +===== 日志 =====
 You can read log messages with: You can read log messages with:
 +
 +**中文翻译**:你可以用下面的命令来读取cron软件执行的记录:
  
 <code bash> <code bash>
Line 78: Line 104:
 Not all messages are logged, to increase logging change ''cronloglevel'' property in [[docs:guide-user:base-system:system_configuration|/etc/config/system]] Not all messages are logged, to increase logging change ''cronloglevel'' property in [[docs:guide-user:base-system:system_configuration|/etc/config/system]]
  
 +**中文翻译**:不是所有信息都会被记录到日志里的,要修改记录的信息等级,可以用“cronloglevel”属性,参见[[docs:guide-user:base-system:system_configuration|/etc/config/system]]文档。
 ===== Extras ===== ===== Extras =====
 +===== 其他 =====
 ==== Periodic reboot ==== ==== Periodic reboot ====
 +==== 定期重启 ====
 A simple solution for some hard-to-solve problems (memory leak, performance degradation, ...) is to reboot the router periodically, for instance every night. A simple solution for some hard-to-solve problems (memory leak, performance degradation, ...) is to reboot the router periodically, for instance every night.
 +
 +**中文翻译**:用来对付一些系统运行时出现的难题(比如内存泄露、性能下降、......)的法宝就是每晚定期重启。
  
 However, this is not as simple as it seems, because the router usually does not have a real-time clock. However, this is not as simple as it seems, because the router usually does not have a real-time clock.
 This could lead to a never-ending loop of reboots. This could lead to a never-ending loop of reboots.
 +
 +**中文翻译**:即便是这样重启,也不是那么简单就能做到的,因为这样的路由器通常没有实时时钟。弄不好就成为下面这个怪圈:
  
 In the boot process the clock is initially set by ''sysfixtime'' to the most recent timestamp of any file found in /etc. In the boot process the clock is initially set by ''sysfixtime'' to the most recent timestamp of any file found in /etc.
Line 90: Line 123:
 Then cron starts and notices a few seconds later that the required boot moment has again arrived and reboots again... Then cron starts and notices a few seconds later that the required boot moment has again arrived and reboots again...
 (At the end of the boot process ntpd starts, and it may also take a while before ntpd gets and sets the correct time, so cron may start the reboot in between.) (At the end of the boot process ntpd starts, and it may also take a while before ntpd gets and sets the correct time, so cron may start the reboot in between.)
 +
 +**中文翻译**:在启动的时候,当前系统的时间使用“sysfixtime”程序来设置;设定依据是在/etc目录下,使用最近被修改的文件的时间;
 +它可能是一个状态文件,或者配置文件,它可能在cron定时任务需要重启系统前30秒刚做了修改。
 +所以,如果在重启系统的过程中,系统时间被设置为这个文件的时间属性的前若干秒钟的话。
 +当cron软件开始计时,cron马上又会发现,设置的重启的时间又到了,然后又开始重启了……
 +(还有的可能就是:启动流程的最后ntpd的时间服务启动了,这个软件得需要一阵子才能获取并且设置正确的时间,所以这段时间内,cron定时重启任务又被触发重启了。)
  
 One solution for cron is to use a delay and touch a file in ''/etc'' before reboot. One solution for cron is to use a delay and touch a file in ''/etc'' before reboot.
 +
 +**中文翻译**:一种解决cron定时重启任务的怪圈的办法是在重启前,推迟一会儿重启,同时主动用touch命令去修改/etc下的任意文件。
  
 <code bash> <code bash>
Line 98: Line 139:
 # and touch a file in /etc so clock will be set # and touch a file in /etc so clock will be set
 # properly to 4:31 on reboot before cron starts. # properly to 4:31 on reboot before cron starts.
 +30 4 * * * sleep 70 && touch /etc/banner && reboot
 +</code>
 +
 +**中文翻译**:举例crontab的配置如下:
 +<code bash>
 +# 每天上午4:30执行重启
 +# 注意: 为了防止循环重启的怪圈,需要推迟70秒钟执行重启
 +# 在/etc目录下touch修改一个文件的时间属性,并且设置
 +# 为上午4:31然后再执行cron的重启任务.
 30 4 * * * sleep 70 && touch /etc/banner && reboot 30 4 * * * sleep 70 && touch /etc/banner && reboot
 </code> </code>
Line 104: Line 154:
 There is usually no programmable circuitry to actually power off the unit. There is usually no programmable circuitry to actually power off the unit.
 ''reboot'' does work, in case you should want to reboot the router periodically. ''reboot'' does work, in case you should want to reboot the router periodically.
 +
 +**中文翻译**::!: 在一些平台上“shutdown”命令是没有的;它仅仅是停止CPU的运行,而不会重启设备。
 +这通常是因为这样的设备没有去执行断电的电路。
  
 However, a more flexible approach is to use use the [[packages:pkgdata:watchcat|watchcat]] package. However, a more flexible approach is to use use the [[packages:pkgdata:watchcat|watchcat]] package.
 +
 +**中文翻译**:即便这样,也还是可以使用一种更为灵活的方法,通过使用[[packages:pkgdata:watchcat|watchcat]]软件包来使用 。
 +安装命令如下:
  
 <code bash> <code bash>
Line 113: Line 169:
  
 ==== Alarm clock ==== ==== Alarm clock ====
 +==== 闹钟功能 ====
 If you have [[wp>Daylight saving time]] you could write yourself a nice alarm clock ;-) If you have [[wp>Daylight saving time]] you could write yourself a nice alarm clock ;-)
 When DST starts in central Europe, clocks advance from 02:00 CET to 03:00 CEST on last Sunday in March. When DST starts in central Europe, clocks advance from 02:00 CET to 03:00 CEST on last Sunday in March.
Line 118: Line 175:
 Later won't work, you'll be late ;-) Later won't work, you'll be late ;-)
 When DST ends in central Europe, clocks retreat from 03:00 CEST to 02:00 CET on last Sunday in October. When DST ends in central Europe, clocks retreat from 03:00 CEST to 02:00 CET on last Sunday in October.
 +
 +**中文翻译**:如果你所在的地区使用“夏令时”[[wp>Daylight saving time]],你可以自己动手写一个合适的闹钟程序;)
 +假设你在欧洲中部使用“夏令时”,那么时钟在每年3月份,就要提前从中欧时间的2点,变成了3点的中欧夏令时时间。
 +在此之前的6天,你可以把自己的网络唤醒闹钟每天提前10分钟。
 +如果没生效的话,估计你上班就要迟到了;)
  
 <code bash> <code bash>
Line 125: Line 187:
 </code> </code>
  
 +**中文翻译**:
 +
 +<code bash>
 +#分 时 日 月 星 执行命令
 +59 05 * * 1 /usr/bin/wol -h 192.168.1.255 xx:xx:xx:xx:xx:xx # Mo
 +#crontab配置文件必须包含一行空白(跟/etc/fstab配置文件一样,该文件用于开机挂在分区)
 +</code>
 +
 +
 +==== 翻译信息 ====
 +LongGenxing 20191103 第一次翻译
  • Last modified: 2020/05/16 16:23
  • by huangzulin