zoukankan      html  css  js  c++  java
  • centos7 crontab 定时执行python任务不执行的原因及解决办法

    1、问题描述

    在用crontab设置定时任务时,发现py脚本在crontab中报错,显示import某些包找不到,但是手动直接运行py脚本,完全正常。
     
    01 05 * * * ./get_topic_hourly.sh >> get_topic_hourly.log 2>&1

    2、问题解析

    主要是crontab的运行目录和py脚本运行目录不一样(直接执行和通过任务执行,程序的 执行环境不同),更改py头的目录,直接指定当前系统中Python的目录,即更改py脚本中默认的头为 Python 安装 目录“!/usr/local/bin/python”,具体根据自己的目录更改
    查看python的安装目录可以用 which python 命令
     
    若以上设置了依旧不执行可以尝试下面的方式执行python

    * * * * * cd /var/www/clientfolder/ && /usr/bin/python /var/www/clientfolder/your_python_script.py
    原因是(直接拷贝):
    Script is location-sensitive. This is related to always using absolute paths in a script, but not quite the same. Your cron job may need to cd to a specific directory before running, e.g. a rake task on a Rails application may need to be in the application root for Rake to find the correct task, not to mention the appropriate database configuration, etc
    我分析可能是:直接执行脚本,脚本执行环境是脚本所在的目录,而定时的脚本执行环境默认是root目录,而当python脚本设置到相对文件路径时就不能执行了。
  • 相关阅读:
    指针和引用的区别
    c++空指针 和 野指针
    strcpy源码实现方式
    函数的分文件编写
    哈夫曼编码实现
    错误:The selected wizard could not be started Plug-in com.genuitec.eclipse.j2ee.ui was unable to load class com.genuitec.eclipse.j2ee.ui.wizard.WebProjectWizard
    sql server,mysql 和navicat for mysql的区别
    MySQL 5.7
    sql server 2017
    Download
  • 原文地址:https://www.cnblogs.com/imustun/p/9604722.html
Copyright © 2011-2022 走看看