当前第2页 返回上一页
12.2 crondtab file
crondtab file为任务定义文件。
(1).在此文件中,空行会被忽略,首个非空白字符且以#开头的行为注释行,但#不能出现在行中。
(2).可以在crontab file中设置环境变量,方式为"name=value",等号两边的空格可随意,即"name = value"也是允许的。但value中出现的空格必须使用引号包围。
(3). 默认crond命令启动的时候会初始化所有变量,除了某几个变量会被crond daemon自动设置好,其他所有变量都被设置为空值。自动设置的变量包括SHELL=/bin/sh,以及HOME和LOGNAME(在CentOS上则称为USER),后两者将被默认设置为/etc/passwd中指定的值。其中SHELL和HOME可以被crontab file中自定义的变量覆盖,但LOGNAME不允许覆盖。当然,自行定义的变量也会被加载到内存。
(4).除了LOGNAME/HOME/SHELL变量之外,如果设置了发送邮件,则crond还会寻找MAILTO变量。如果设置了MAILTO,则邮件将发送给此变量指定的地址,如果MAILTO定义的值为空(MAILTO=""),将不发送邮件,其他所有情况邮件都会发送给crontab file的所有者。
(5).在系统定时任务文件/etc/crontab中,默认已定义PATH环境变量和SHELL环境变量,其中PATH=/sbin:/bin:/usr/sbin:/usr/bin。
(6).crond daemon每分钟检测一次crontab file看是否有任务计划条目需要执行。
12.3 crond命令的调试
很多时候写了定时任务却发现没有执行,或者执行失败,但因为crond是后台运行的,有没有任何提示,很难进行排错。但是可以让crond运行在前端并进行调试的。
先说明下任务计划程序crond的默认执行方式。
使用下面三条命令启动的crond都是在后台运行的,且都不依赖于终端。
1 2 3 | [root@xuexi ~]# systemctl start crond.service
[root@xuexi ~]# service crond start
[root@xuexi ~]# crond
|
但crond是允许接受选项的。
1 2 3 | crond [-n] [-P] [-x flags]
选项说明:-n:让crond以前端方式运行,即不依赖于终端。-P:不重设环境变量PATH,而是从父进程中继承。-x:设置调试项,flags是调试方式,比较有用的方式是test和sch,即 "-x test" 和 "-x sch" 。
:其中test调试将不会真正的执行,sch调试将可以看到等待时间。具体的见下面的示例。
|
先看看启动脚本启动crond的方式。
1 2 3 4 5 6 7 8 9 10 11 | [root@server2 ~]# cat /lib/systemd/system/crond.service
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target
[Service]EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGSExecReload =/bin/kill -HUP $MAINPID
KillMode=process
[Install]
WantedBy=multi-user.target
|
它的环境配置文件为/etc/sysconfig/crond,该文件中什么也没设置。
1 2 3 4 | [root@server2 ~]# cat /etc/sysconfig/crond
# Settings for the CRON daemon.
# CRONDARGS= : any extra command-line startup arguments for crond
CRONDARGS=
|
所有它的启动命令为:/usr/sbin/crond -n。但尽管此处加了"-n"选项,crond也不会前端运行,且不会依赖于终端,这是systemctl决定的。
在解释下如何进行调试。以下面的任务条目为例。
1 | [root@server2 ~]# crontab -e* * * * * echo "hello world" >>/tmp/hello.txt
|
执行crond并带上调试选项test。
1 2 3 4 5 6 | [root@server2 ~]# crond -x test
debug flags enabled: test
[4903] cron started
log_it: (CRON 4903) INFO (RANDOM_DELAY will be scaled with factor 8% if used.)
log_it: (CRON 4903) INFO (running with inotify support)
log_it: (CRON 4903) INFO (@reboot jobs will be run at computer's startup.)log_it: (root 4905) CMD ( echo "hello world" >>/tmp/hello.txt )
|
执行crond并带上调试选项sch。
1 2 3 4 5 6 7 8 | [root@server2 ~]# crond -x sch
debug flags enabled: sch
[4829] cron started
log_it: (CRON 4829) INFO (RANDOM_DELAY will be scaled with factor 73% if used.)
log_it: (CRON 4829) INFO (running with inotify support)
[4829] GMToff=28800log_it: (CRON 4829) INFO (@reboot jobs will be run at computer's startup.)[4829] Target time=1497950880, sec-to-wait=38 # 等待crond daemon下一次的检测,所以表示38秒后crond将检测crontab file
user [root:0:0:...] cmd= "echo " hello world " >>/tmp/hello.txt "
[4829] Target time=1497950940, sec-to-wait=60Minute-ly job. Recording time 1497922081log_it: (root 4831) CMD ( echo "hello world" >>/tmp/hello.txt )user [root:0:0:...] cmd= "echo " hello world " >>/tmp/hello.txt " [4829] Target time=1497951000, sec-to-wait=60Minute-ly job. Recording time 1497922141log_it: (root 4833) CMD ( echo "hello world" >>/tmp/hello.txt )
|
但要注意,在sch调试结果中的等待时间是crond这个daemon的检测时间,所以它表示等待下一次检测的时间,因此除了第一次,之后每次都是60秒,因为默认crond是每分钟检测一次crontab file的。例如,下面是某次的等待结果,在这几次等待检测过程中没有执行任何任务。
1 | [4937] Target time=1497951720, sec-to-wait=18[4937] Target time=1497951780, sec-to-wait=60[4937] Target time=1497951840, sec-to-wait=60
|
还可以同时带多个调试方式,如:
1 2 3 4 5 6 | [root@server2 ~]# crond -x test,schdebug flags enabled: sch test[4914] cron started
log_it: (CRON 4914) INFO (RANDOM_DELAY will be scaled with factor 21% if used.)
log_it: (CRON 4914) INFO (running with inotify support)
[4914] GMToff=28800log_it: (CRON 4914) INFO (@reboot jobs will be run at computer's startup.)[4914] Target time=1497951540, sec-to-wait=9
user [root:0:0:...] cmd= "echo " hello world " >>/tmp/hello.txt "
[4914] Target time=1497951600, sec-to-wait=60Minute-ly job. Recording time 1497922741log_it: (root 4916) CMD ( echo "hello world" >>/tmp/hello.txt )
|
这样在调试定时任务时间时,也不会真正执行命令。
12.4 精确到秒的任务计划
默认情况下,crond执行的任务只能精确到分钟,无法精确到秒。但通过技巧,也是能实现秒级任务的。
(1).方法一:不太精确的方法
写一个脚本,在脚本中sleep3秒钟的时间,这样能实现每3秒执行一次命令。
1 2 3 | [root@xuexi ~]# cat /tmp/a.sh#!/bin/bash
#
PATH= "$PATH:/usr/local/bin:/usr/local/sbin" for ((i=1;i<=20;i++));dols /tmpsleep 3done
|
1 | [root@xuexi ~]# cat / var /spool/cron/lisi* * * * * /bin/bash /tmp/a.sh
|
但是这样的方法不是最佳方法,因为执行命令也需要时间,且crond默认会有一个随机延时,随机延时由变量RANDOM_DELAY定义。
(2).方法二:在cron配置文件中写入多条sleep命令和其他命令。
1 2 | [root@xuexi ~]# cat / var /spool/cron/lisi* * * * * ls /tmp* * * * * sleep 3 && ls /tmp* * * * * sleep 6 && ls /tmp* * * * * sleep 9 && ls /tmp* * * * * sleep 12 && ls /tmp* * * * * sleep 15 && ls /tmp* * * * * sleep 18 && ls /tmp* * * * * sleep 21 && ls /tmp* * * * * sleep 24 && ls /tmp* * * * * sleep 27 && ls /tmp* * * * * sleep 30 && ls /tmp
…* * * * * sleep 57 && ls /tmp
|
这种方式很繁琐,但是更精确。如果定义到每秒级别就得写60行cron记录。
由此能看出,秒级的任务本就不是crond所擅长的。实际上能用到秒级的任务也比较少。
以上就是Linux讲解之定时任务的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
rsync定期更新配置方法
linux讲解之定时任务
rsync定期更新配置方法
linux讲解之定时任务
更多相关阅读请进入《12章》频道 >>
转载请注明出处:木庄网络博客 » Linux讲解之定时任务