关键词:crontab不执行,crontab注意事项

问题:

如果命令在终端正常,但是crontab不执行,是因为crontab无法读取环境变量

解决方法:

  • 方法一:所有命令需要写成绝对路径形式,如: /usr/local/bin/docker。
  • 方法二:在 shell 脚本开头使用以下代码:
    #!/bin/sh
    
    . /etc/profile
    . ~/.bash_profile
    
  • 方法三:在 /etc/crontab 中添加环境变量,在可执行命令之前添加命令 . /etc/profile;/bin/sh,使得环境变量生效,例如:20 03 * * * . /etc/profile;/bin/sh /var/www/wwwroot/test.sh

另外:内置的调度

cron默认配置了调度任务,分别为:hourly、daily、weekly、mouthly,默认配置文件为/etc/anacrontab。将需要执行的脚本放到相应的目录下即可,目录分别为:
/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/ect/cron.mouthly

参考链接:https://blog.csdn.net/m0_49605975/article/details/120701771