前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux中定时任务(下)

Linux中定时任务(下)

原创
作者头像
陈不成i
修改2021-05-27 14:32:27
1.4K0
修改2021-05-27 14:32:27
举报
文章被收录于专栏:ops技术分享

三.crond命令的调试

很多时候写了定时任务却发现没有执行,或者执行失败,但因为crond是后台运行的,有没有任何提示,很难进行排错。但是可以让crond运行在前端并进行调试的。

先说明下任务计划程序crond的默认执行方式。

使用下面三条命令启动的crond都是在后台运行的,且都不依赖于终端。

  1. [root@xuexi ~]# systemctl start crond.service
  2. [root@xuexi ~]# service crond start
  3. [root@xuexi ~]# crond

但crond是允许接受选项的。

  1. crond [-n] [-P] [-x flags]
  2. 选项说明:
  3. -n:让crond以前端方式运行,即不依赖于终端。
  4. -P:不重设环境变量PATH,而是从父进程中继承。
  5. -x:设置调试项,flags是调试方式,比较有用的方式是test和sch,即"-x test"和"-x sch"。
  6. :其中test调试将不会真正的执行,sch调试显示调度信息,可以看到等待时间。具体的见下面的示例。

先看看启动脚本启动crond的方式。

  1. [root@server2 ~]# cat /lib/systemd/system/crond.service
  2. [Unit]
  3. Description=Command Scheduler
  4. After=auditd.service systemd-user-sessions.service time-sync.target
  5. [Service]
  6. EnvironmentFile=/etc/sysconfig/crond
  7. ExecStart=/usr/sbin/crond -n $CRONDARGS
  8. ExecReload=/bin/kill -HUP $MAINPID
  9. KillMode=process
  10. [Install]
  11. WantedBy=multi-user.target

它的环境配置文件为/etc/sysconfig/crond,该文件中什么也没设置。

  1. [root@server2 ~]# cat /etc/sysconfig/crond
  2. # Settings for the CRON daemon.
  3. # CRONDARGS= : any extra command-line startup arguments for crond
  4. CRONDARGS=

所有它的启动命令为:/usr/sbin/crond -n。但尽管此处加了”-n”选项,crond也不会前端运行,且不会依赖于终端,这是systemctl决定的。

再解释下如何进行调试。以下面的任务条目为例。

  1. [root@server2 ~]# crontab -e
  2. * * * * * echo "hello world" >>/tmp/hello.txt

执行crond并带上调试选项test。

  1. [root@server2 ~]# crond -x test
  2. debug flags enabled: test
  3. [4903] cron started
  4. log_it: (CRON 4903) INFO (RANDOM_DELAY will be scaled with factor 8% if used.)
  5. log_it: (CRON 4903) INFO (running with inotify support)
  6. log_it: (CRON 4903) INFO (@reboot jobs will be run at computer's startup.)
  7. log_it: (root 4905) CMD (echo "hello world" >>/tmp/hello.txt )

执行crond并带上调试选项sch。

  1. [root@server2 ~]# crond -x sch
  2. debug flags enabled: sch
  3. [4829] cron started
  4. log_it: (CRON 4829) INFO (RANDOM_DELAY will be scaled with factor 73% if used.)
  5. log_it: (CRON 4829) INFO (running with inotify support)
  6. [4829] GMToff=28800
  7. log_it: (CRON 4829) INFO (@reboot jobs will be run at computer's startup.)
  8. [4829] Target time=1497950880, sec-to-wait=38 # 等待crond daemon下一次的检测,所以表示38秒后crond将检测crontab file
  9. user [root:0:0:...] cmd="echo "hello world" >>/tmp/hello.txt "
  10. [4829] Target time=1497950940, sec-to-wait=60
  11. Minute-ly job. Recording time 1497922081
  12. log_it: (root 4831) CMD (echo "hello world" >>/tmp/hello.txt )
  13. user [root:0:0:...] cmd="echo "hello world" >>/tmp/hello.txt "
  14. [4829] Target time=1497951000, sec-to-wait=60
  15. Minute-ly job. Recording time 1497922141
  16. log_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
  2. [4937] Target time=1497951780, sec-to-wait=60
  3. [4937] Target time=1497951840, sec-to-wait=60

还可以同时带多个调试方式,如:

  1. [root@server2 ~]# crond -x test,sch
  2. debug flags enabled: sch test
  3. [4914] cron started
  4. log_it: (CRON 4914) INFO (RANDOM_DELAY will be scaled with factor 21% if used.)
  5. log_it: (CRON 4914) INFO (running with inotify support)
  6. [4914] GMToff=28800
  7. log_it: (CRON 4914) INFO (@reboot jobs will be run at computer's startup.)
  8. [4914] Target time=1497951540, sec-to-wait=9
  9. user [root:0:0:...] cmd="echo "hello world" >>/tmp/hello.txt "
  10. [4914] Target time=1497951600, sec-to-wait=60
  11. Minute-ly job. Recording time 1497922741
  12. log_it: (root 4916) CMD (echo "hello world" >>/tmp/hello.txt )

这样在调试定时任务时间时,也不会真正执行命令。

四.精确到秒的任务计划

默认情况下,crond执行的任务只能精确到分钟,无法精确到秒。但通过技巧,也是能实现秒级任务的。

(1).方法一:不太精确的方法

写一个脚本,在脚本中sleep3秒钟的时间,这样能实现每3秒执行一次命令。

  1. [root@xuexi ~]# cat /tmp/a.sh
  2. #!/bin/bash
  3. #
  4. PATH="$PATH:/usr/local/bin:/usr/local/sbin"
  5. for ((i=1;i<=20;i++));do
  6. ls /tmp
  7. sleep 3
  8. done

(2).方法二:在cron配置文件中写入多条sleep命令和其他命令。

  1. [root@xuexi ~]# cat /var/spool/cron/lisi
  2. * * * * * ls /tmp
  3. * * * * * sleep 3 && ls /tmp
  4. * * * * * sleep 6 && ls /tmp
  5. * * * * * sleep 9 && ls /tmp
  6. * * * * * sleep 12 && ls /tmp
  7. * * * * * sleep 15 && ls /tmp
  8. * * * * * sleep 18 && ls /tmp
  9. * * * * * sleep 21 && ls /tmp
  10. * * * * * sleep 24 && ls /tmp
  11. * * * * * sleep 27 && ls /tmp
  12. * * * * * sleep 30 && ls /tmp
  13. * * * * * sleep 57 && ls /tmp

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 三.crond命令的调试
  • 四.精确到秒的任务计划
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档