zoukankan      html  css  js  c++  java
  • crontab采坑总结

    目录

    crontab是Linux平台实现定时任务的服务工具,通常情况下该服务会预装在发行版中,直接使用即可。

    关于crontab的详细用法参考:https://man7.org/linux/man-pages/man5/crontab.5.html ,网络上也有许多博文介绍,不再赘述。

    在此主要阐述在使用crontab时容易遇到的几个采坑问题:
    1.crontab环境变量
    2.脚本缺少执行权限

    图片来源于网络

    crontab环境变量

    在使用crontab时遇到的环境变量问题表现为:
    在shell中能正常执行的脚本,但是通过crontab任务调度时就提示命令不存在:Message: 'xxx' executable needs to be in PATH.
    例如,我的shell脚本运行Selenium python程序时就遇到如下报错:

    selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.
    

    产生该问题的本质原因是:crontab任务在执行时所能读取到环境变量与用户登录后所读取到到的环境变量是不同的。
    用户登录shell后所能读取到的环境变量通常定义在如下几个地方:~/.bashrc/etc/profile,而crontab定时任务所能读取到的环境变量是定义在 /etc/crontab中的。
    如下是我的Linux主机的/etc/crontab内容:

    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # For details see man 4 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) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed
    

    如上所示,crontab任务所能读取到的PATH变量在如下路径中:/sbin:/bin:/usr/sbin:/usr/bin
    也就是说在crontab任务中运行的命令都需要在这些路劲下,否则就会提示路径找不到的报错信息。

    如下还是以我遇到的‘chromedriver’命令找不到的问题为例进行说明:

    首先,查看一下chromedriver命令所在的路径。

    [root@test ~]# whereis chromedriver
    chromedriver: /usr/local/bin/chromedriver
    

    显然/usr/local/bin/chromedriver并不在/etc/crontab所定义的PATH路径中,所以找不到命令很正常。

    解决办法如下:

    办法一:

    将chromedriver移动到/usr/bin路径下:

    [root@test ~]# mv /usr/local/bin/chromedriver /usr/bin/
    [root@test ~]# whereis chromedriver
    chromedriver: /usr/bin/chromedriver
    

    办法二:

    在crontab任务执行的脚本中将chromedriver添加到PATH变量中

    # 先加载环境变量
    export PATH=$PATH:/usr/local/bin/chromedriver
    # 再执行命令
    ...
    

    办法三:

    /usr/local/bin/chromedriver添加到/etc/profile或者~/.bashrc中,然后在crontab任务的执行命令中动态加载环境变量。

    [root@test ~]# crontab -l
    5 0 * * * source /etc/profile && /root/task/test.sh
    

    脚本缺少执行权限

    当脚本缺少可执行权限时,crontab任务无法调度执行。

    [root@test task]# ls -l
    -rw-r--r-- 1 root root  96 Jul 28 17:12 task_test.sh
    

    此时表现出的现象就是脚本没有按照设置的定时参数执行,看起来就像是定时参数设置不正确似的。
    注意: 这个问题很有迷惑性,会让人觉得是定时参数设置不正确,所以在使用crontab任务调度脚本执行时,首先要做的事情就是给脚本添加可执行权限。

    [root@test task]# chmod +x task_test.sh
    [root@test task]# ls -l
    -rwxr-xr-x 1 root root  96 Jul 28 17:12 task_test.sh
    

    另外,关于crontab的定时参数的设置,可以通过2个在线工具完成,非常方便:
    https://tool.lu/crontab
    https://crontab.guru/

    【参考】
    https://www.ibm.com/support/pages/cron-environment-and-cron-job-failures The Cron Environment and Cron Job Failures
    https://www.baeldung.com/linux/load-env-variables-in-cron-job How to Load Environment Variables in a Cron Job
    https://www.jianshu.com/p/446758d924df 【crontab】crontab 与 环境变量
    https://www.cnblogs.com/zhenglisai/p/6846372.html 【linux】crontab的环境变量问题
    https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html crontab 定时任务


    作者:编程随笔
    出处:http://www.cnblogs.com/nuccch/
    声明:本文版权归作者和博客园共有,欢迎转载,但请在文章页面明显位置给出原文连接。

  • 相关阅读:
    php 配置文件
    读MBA经历回顾(下)做法决定结果——北漂18年(49)
    虚拟化的发展历程和实现方式
    虚拟化的发展历程和实现方式
    Rhino -- 基于java的javascript实现
    php mvc 框架演示
    Web报表工具FineReport二次开发JS之字符串
    红帽虚拟化RHEV-PXE批量安装RHEV-H
    红帽虚拟化RHEV-PXE批量安装RHEV-H
    css 样式
  • 原文地址:https://www.cnblogs.com/nuccch/p/15072469.html
Copyright © 2011-2022 走看看