首页 >> 分享>>Ubuntu>>Ubuntu 定时任务crontab不执行问题
Ubuntu 定时任务crontab不执行问题
Linux 定时任务常用crontab来周期性定时执行某个任务。
默认配置文件在:/et/crontab,Ubuntu有如下配置文件,可以每天/每小时/每月/每周进行事物处理,我们通常把需要处理的脚本放入对应的文件夹即可。
常用文件夹如下: cron.d/ cron.daily/ cron.hourly/ cron.monthly/ cron.weekly/
最近有个任务需要每天执行,所以尝试在cron.daily里面新增脚本,但发现脚本并没有如期运行。
遂做排查如下:
- 检查文件是否有可执行权限 有执行权限
- 尝试在脚本头添加
#!/bin/bash无效 - 查看 /etc/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
# You can also override PATH, but by default, newer versions inherit it from the environment
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# 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) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
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 )
#
- 手动执行脚本
test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )仍然无效 - 仔细查看区别 ls -lA /etc/cron.daily
发现我自定义脚本多了custom.sh .sh 后缀,Google查询得知道 run-parts 不支持该后嘴,移除后再次尝试脚本正常执行
总结:
- run-parts 命令会执行指定文件目录所有脚本
- run-parts 不支持
.sh后缀文件 test -x /usr/sbin/anacron如果anacron存在并可执行,就不执行后续命令
分享一个定时任务编辑工具:https://crontab.guru/
we2lsi
r10g7g
l9jw6r
pshw71
3mp0sn
fsybhe
e8rz38
002g6v
moyx4f
mm8ae2
rbmgmx
8t0m5i
3sphox
u2phcf
x7j63o
ns45y9
0kk6gq
eia01j
rl16ia
9k3sx4
0fg6ak
fk192r
rqpyoj
hp6too
8epsbt
yr0c8h
gudx6y
htft31
uwht9u
wmwbce
icie32
ssx1ba
atyrw8
iyspz1
d06wbt
akswvn
32baqt