最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Ubuntu中crontab的脚本任务没有执行

执行 crifan 2482浏览 0评论
折腾:
【已解决】小程序的ssl证书即将过期
期间,之前加到crontab中的任务:
root@VM-158-80-ubuntu:~/xxx# crontab -l
*/20 * * * * /usr/sbin/ntpdate ntpupdate.tencentyun.com >/dev/null &
*/1 * * * * /usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &
0 4 * * 0 /root/xxx/renew_cert.sh >/dev/null 2>&1
0 0 * * * /usr/local/qcloud/YunJing/YDCrontab.sh > /dev/null 2>&1 &
其中:
0 4 * * 0 /root/xxx/renew_cert.sh >/dev/null 2>&1
是去更新ssl证书的
但是过了3个月后,还是没有更新。
感觉是crontab的任务没有运行。
最后只好手动执行命令
sudo sh /root/xxx/renew_cert.sh
才更新的。
而此处要去搞清楚为何没有执行
cron not work
cron – Why crontab scripts are not working? – Ask Ubuntu
linux – Why is my crontab not working, and how can I troubleshoot it? – Server Fault
crontab – Cron Job Not Running? – Ask Ubuntu
18.04 – Cron not working, although the command works – Ask Ubuntu
难道是此处脚本权限不对?
root@VM-158-80-ubuntu:~/xxx# ll
total 64
drwxr-xr-x  3 root root  4096 Jun  7 11:07 ./
drwx------ 12 root root  4096 Aug 23 19:42 ../
-rw-r--r--  1 root root  3247 Aug 21  2018 account.key
-rw-r--r--  1 root root 11345 Aug 21  2018 acme_tiny.py
-rw-r--r--  1 root root  5559 Nov  8 23:07 chained.pem
drwxr-xr-x  2 root root  4096 Nov  8 23:06 challenges/
-rw-r--r--  1 root root  1622 Aug 21  2018 domain.csr
-rw-r--r--  1 root root  3243 Aug 21  2018 domain.key
-rw-r--r--  1 root root  3614 Aug 21  2018 full_chained.pem
-rw-r--r--  1 root root  1647 Nov  8 23:07 intermediate.pem
-rw-r--r--  1 root root   304 Aug 21  2018 renew_cert.sh
-rw-r--r--  1 root root  1967 Aug 21  2018 root.pem
-rw-r--r--  1 root root  3912 Nov  8 23:07 signed.crt
好像此处renew_cert.sh是没有可执行权限
去加上:
root@VM-158-80-ubuntu:~/xxx# chmod +x renew_cert.sh 
root@VM-158-80-ubuntu:~/xxx# ll renew_cert.sh 
-rwxr-xr-x 1 root root 304 Aug 21  2018 renew_cert.sh*
去看看cron文件
root@VM-158-80-ubuntu:~/xxx# ll /etc/cron*
-rw-r--r-- 1 root root  722 Apr  6  2016 /etc/crontab


/etc/cron.d:
total 32
drwxr-xr-x   2 root root  4096 Jun 18 21:30 ./
drwxr-xr-x 116 root root 12288 Sep 26 10:19 ../
-rw-r--r--   1 root root   589 Jul 16  2014 mdadm
-rw-r--r--   1 root root   712 Jan  9  2017 php
-rw-r--r--   1 root root   102 Apr  6  2016 .placeholder
-rw-r--r--   1 root root   190 Oct 26  2016 popularity-contest


/etc/cron.daily:
total 72
drwxr-xr-x   2 root root  4096 Jun 18 21:31 ./
drwxr-xr-x 116 root root 12288 Sep 26 10:19 ../
-rwxr-xr-x   1 root root   539 Apr  6  2016 apache2*
-rwxr-xr-x   1 root root   376 Mar 31  2016 apport*
-rwxr-xr-x   1 root root  1474 May  7  2019 apt-compat*
-rwxr-xr-x   1 root root   355 May 22  2012 bsdmainutils*
-rwxr-xr-x   1 root root  1597 Nov 27  2015 dpkg*
-rwxr-xr-x   1 root root   372 May  6  2015 logrotate*
-rwxr-xr-x   1 root root  1293 Nov  6  2015 man-db*
-rwxr-xr-x   1 root root   539 Jul 16  2014 mdadm*
-rwxr-xr-x   1 root root   435 Nov 18  2014 mlocate*
-rwxr-xr-x   1 root root   249 Nov 13  2015 passwd*
-rw-r--r--   1 root root   102 Apr  6  2016 .placeholder
-rwxr-xr-x   1 root root  3449 Feb 27  2016 popularity-contest*
-rwxr-xr-x   1 root root   383 Mar  8  2016 samba*
-rwxr-xr-x   1 root root   214 May 25  2016 update-notifier-common*


/etc/cron.hourly:
total 20
drwxr-xr-x   2 root root  4096 Oct 26  2016 ./
drwxr-xr-x 116 root root 12288 Sep 26 10:19 ../
-rw-r--r--   1 root root   102 Apr  6  2016 .placeholder


/etc/cron.monthly:
total 20
drwxr-xr-x   2 root root  4096 Oct 26  2016 ./
drwxr-xr-x 116 root root 12288 Sep 26 10:19 ../
-rw-r--r--   1 root root   102 Apr  6  2016 .placeholder


/etc/cron.weekly:
total 32
drwxr-xr-x   2 root root  4096 Jun 18 21:31 ./
drwxr-xr-x 116 root root 12288 Sep 26 10:19 ../
-rwxr-xr-x   1 root root    86 Apr 14  2016 fstrim*
-rwxr-xr-x   1 root root   771 Nov  6  2015 man-db*
-rw-r--r--   1 root root   102 Apr  6  2016 .placeholder
-rwxr-xr-x   1 root root   211 May 25  2016 update-notifier-common*
看到:
root@VM-158-80-ubuntu:~/xxx# cat /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.


SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin


# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
没有我们的
然后找到了:
root@VM-158-80-ubuntu:~/xxx# ll /var/spool/cron/
atjobs/   atspool/  crontabs/ 
root@VM-158-80-ubuntu:~/xxx# ll /var/spool/cron/crontabs/root 
-rw------- 1 root crontab 438 Sep 26 10:19 /var/spool/cron/crontabs/root
root@VM-158-80-ubuntu:~/xxx# cat /var/spool/cron/crontabs/root
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Thu Sep 26 10:19:54 2019)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
*/20 * * * * /usr/sbin/ntpdate 
ntpupdate.tencentyun.com
 >/dev/null &
*/1 * * * * /usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &
0 4 * * 0 /root/xxx/renew_cert.sh >/dev/null 2>&1
0 0 * * * /usr/local/qcloud/YunJing/YDCrontab.sh > /dev/null 2>&1 &
另外拷贝过来别人的语法解释
有2种:
(1)User crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *   command to be executed
(2)System wide /etc/crontab and /etc/cron.d fragments
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
另外:
* 号表示任意
, 逗号分割表示时刻, separator
- 短横线连接,表示时间段, range of values
/ 表示间隔, 如果第一个域为 /2 ,则表示每隔两分钟, step value
此处,之前语法是:
0 4 * * 0 /root/xxx/renew_cert.sh >/dev/null 2>&1
此处都搞不清之前写法的含义了
希望是:
每1个月或2个月执行一次
crontab 语法
crontab 命令,Linux crontab 命令详解:提交和管理用户的需要周期性执行的任务 – Linux 命令搜索引擎
cron语法格式学习 – 简书
19. crontab 定时任务 — Linux Tools Quick Tutorial
每天学习一个命令:crontab 定时任务 | Verne in GitHub
想要每个月一次
此处难道是:
0 0 * */2 * /root/xxx/renew_cert.sh >/dev/null 2>&1
不清楚
突然想到,会不会有在线帮忙生成的
crontab 含义 在线
crontab执行时间计算 – 在线工具
好像不对
quartz/Cron/Crontab表达式在线生成工具-BeJSON.com
http://www.bejson.com/othertools/cron/
最后在
https://tool.lu/crontab/
尝试半天,终于得到我们要的:
0 3 1 */2 *
接下来7次的执行时间:
1. 2019-12-01 03:00:00
2. 2020-02-01 03:00:00
3. 2020-04-01 03:00:00
4. 2020-06-01 03:00:00
5. 2020-08-01 03:00:00
6. 2020-10-01 03:00:00
7. 2020-12-01 03:00:00
-》
接下来的每个偶数的月份的1号的凌晨3:00
总体频率:每2月一次
而对于之前的:
0 4 * * 0
接下来7次的执行时间:
1. 2019-11-10 04:00:00
2. 2019-11-17 04:00:00
3. 2019-11-24 04:00:00
4. 2019-12-01 04:00:00
5. 2019-12-08 04:00:00
6. 2019-12-15 04:00:00
7. 2019-12-22 04:00:00
即:
接下来的
每周日的04:00
都执行,即每周1次,频率更高
只不过此处估计是crontab失效的原因,导致命令没有运行成功而已。
至此,去编辑一下:
root@VM-158-80-ubuntu:~/xxx# crontab -e
*/20 * * * * /usr/sbin/ntpdate ntpupdate.tencentyun.com >/dev/null &
*/1 * * * * /usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &
0 4 1 */2 * /root/xxx/renew_cert.sh >/dev/null 2>&1
0 0 * * * /usr/local/qcloud/YunJing/YDCrontab.sh > /dev/null 2>&1 &
~                                                                                                                                                                         
"crontab.gczI48/crontab" 4L, 265C written                                                                                                               
crontab: installing new crontab
查看log:
root@VM-158-80-ubuntu:~/xxx# tailf /var/log/syslog | grep cron
Nov  8 23:43:31 localhost crontab[14765]: (root) BEGIN EDIT (root)
Nov  8 23:45:22 localhost crontab[14765]: (root) REPLACE (root)
Nov  8 23:45:22 localhost crontab[14765]: (root) END EDIT (root)
Nov  8 23:46:01 localhost cron[899]: (root) RELOAD (crontabs/root)


^C
查看cron的状态
root@VM-158-80-ubuntu:~/xxx# /etc/init.d/cron status
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-05-07 14:07:20 CST; 6 months 2 days ago
     Docs: man:cron(8)
 Main PID: 899 (cron)
    Tasks: 8
   Memory: 213.7M
      CPU: 1d 5h 39min 19.539s
   CGroup: /system.slice/cron.service
           ├─  899 /usr/sbin/cron -f
           ├─ 1694 /usr/local/qcloud/stargate/sgagent -d
           ├─32196 barad_agent
           ├─32202 barad_agent
           └─32203 barad_agent


Nov 08 23:45:01 VM-158-80-ubuntu CRON[14974]: pam_unix(cron:session): session closed for user root
Nov 08 23:46:01 VM-158-80-ubuntu cron[899]: (root) RELOAD (crontabs/root)
Nov 08 23:46:01 VM-158-80-ubuntu CRON[15110]: pam_unix(cron:session): session opened for user root by (uid=0)
Nov 08 23:46:01 VM-158-80-ubuntu CRON[15111]: (root) CMD (/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &)
Nov 08 23:46:01 VM-158-80-ubuntu CRON[15110]: pam_unix(cron:session): session closed for user root
Nov 08 23:47:01 VM-158-80-ubuntu CRON[15237]: pam_unix(cron:session): session opened for user root by (uid=0)
Nov 08 23:47:01 VM-158-80-ubuntu CRON[15238]: (root) CMD (/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &)
Nov 08 23:47:01 VM-158-80-ubuntu CRON[15237]: pam_unix(cron:session): session closed for user root
Nov 08 23:48:01 VM-158-80-ubuntu CRON[15386]: pam_unix(cron:session): session opened for user root by (uid=0)
Nov 08 23:48:01 VM-158-80-ubuntu CRON[15387]: (root) CMD (/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &)
状态是active running,是对的
运行了6个多月了。
后来去验证一下:
干脆再去修改一下,看看能否立刻运行:
# crontab -e
19 0 * * * /root/xxx/renew_cert.sh >/dev/null 2>&1
然后1分钟后,时间到了00:19后
通过status中看到的确运行了:
# service cron status
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-05-07 14:07:20 CST; 6 months 2 days ago
     Docs: man:cron(8)
...
Nov 09 00:19:01 VM-158-80-ubuntu CRON[19887]: (root) CMD (/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &)
Nov 09 00:19:01 VM-158-80-ubuntu CRON[19888]: (root) CMD (/root/xxx/renew_cert.sh >/dev/null 2>&1)
Nov 09 00:19:01 VM-158-80-ubuntu CRON[19886]: pam_unix(cron:session): session closed for user root
那去看看,证书时间是否被更新
# openssl x509 -dates -noout < /root/xxx/chained.pem
notBefore=Nov  8 15:19:26 2019 GMT
notAfter=Feb  6 15:19:26 2020 GMT
其中的15:19中的19,就是对应着上面的00:19中的19,看来就是正常了。
【总结】
至此,的确解决问题了:
(1)确认cron状态正常:正在运行
/etc/init.d/cron status
确认状态是
   Active: active (running) since Tue 2019-05-07 14:07:20 CST; 6 months 2 days ago
(2)也能看到log日志
tailf /var/log/syslog | grep cron
(3)确认cron语法和含义正确
# crontab -l
0 4 1 */2 * /root/xxx/renew_cert.sh >/dev/null 2>&1
表示:
接下来每2个月(偶数月)的1号的04:00运行脚本
(4)确认脚本有权限执行
# ll /root/xxx/renew_cert.sh 
-rwxr-xr-x 1 root root 304 Aug 21  2018 /root/xxx/renew_cert.sh*
脚本/root/xxx/renew_cert.sh已加可执行权限
这下可以正常运行了。
【后记】
【整理】如何读懂cron的值的真正含义即计算出命令执行的具体时间

转载请注明:在路上 » 【已解决】Ubuntu中crontab的脚本任务没有执行

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
87 queries in 0.176 seconds, using 21.87MB memory