zoukankan      html  css  js  c++  java
  • crontab环境变量问题

    今天设置linux定时任务时,python内调用的shell指令总执行失败,单独调用python脚本则无问题,考虑到是环境变量未生效引起。

    故在执行crontab -e编辑配置文件时,将shell内执行env指令得到的环境变量(PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games)加入到任务(31 15 * * *  python /opt/ybs/bin/backup.py  >> /opt/ybs/log/backup.log 2>&1)之前。

    编辑后的文件如下:

    # Edit this file to introduce tasks to be run by cron.
    #
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    #
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').#
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    #
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    #
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    #
    # For more information see the manual pages of crontab(5) and cron(8)
    #
    # m h dom mon dow command
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
    31 15 * * * python /opt/ybs/bin/backup.py >> /opt/ybs/log/backup.log 2>&1

    保存退出。

    实测成功执行。

  • 相关阅读:
    source命令
    bash中单引号与双引号的总结
    鸟哥的linux私房菜学习笔记3
    鸟哥的linux私房菜学习笔记2
    /usr/sbin/atd 和 /etc/init.d/atd有什么区别
    Linux中/etc/fstab /etc/mtab /proc/mounts这三个文件的分析与比较
    服务器本机不能登陆的解决
    有用的博客
    推荐几个好用的PHP集成开发环境
    ANDROID开发笔记(二)
  • 原文地址:https://www.cnblogs.com/wangzhigang/p/5124514.html
Copyright © 2011-2022 走看看