【问题标题】:Crontab using incorrect version of Python to run scriptCrontab 使用不正确的 Python 版本来运行脚本
【发布时间】:2015-09-14 13:31:54
【问题描述】:

crontab 正在使用 2.6 版本来运行需要 2.7 才能运行的脚本。如何将 Python 的默认版本永久设置为 2.7? 运行 ./file.py 工作正常,它只是通过 crontab 运行时

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# 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
*,30 *  *  *  * root /root/file.py >>/tmp/log.txt 2>&1

编辑 问题已解决

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin/python
MAILTO=root
HOME=/

# 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
*,30 *  *  *  * root /usr/local/bin/python2.7 /root/file.py >>/tmp/log.txt 2>&1

【问题讨论】:

  • 你的环境是否安装了python 2.7?
  • 是的,我可以运行 ./file.py 很好,只是当它通过 crontab 运行时它是一个问题
  • python2.7安装在哪里?
  • 在你的 shell 类型中确定 python 并使用路径并将其添加为 PATH 变量中的第一个路径
  • 在命令行尝试which pythonwhich python2.7

标签: python python-2.7 crontab


【解决方案1】:

更新 Python 脚本的 shebang 以明确使用 Python 2.7:

#!/usr/bin/env python2.7

第一行告诉 Unix 使用哪个解释器。

【讨论】:

    【解决方案2】:

    如果您为 2.7 安装了单独的 python 版本,您可以使用 whereis python 查找它

    我有 CentOS 6,默认是 2.6,所以这个命令返回:

    python2: /usr/bin/python2.6 /usr/bin/python2.6-config /usr/bin/python2 /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python2.7-config /usr/local/bin/python2.7 /usr/local/lib/python2.7 /usr/include/python2.6
    

    其中/usr/local/bin/python2.7是我感兴趣的,所以当我在crontab中放置工作时,我会明确选择它:

    30 00 * * * /usr/local/bin/python2.7 /home/mike/job.py
    

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 1970-01-01
      • 2015-10-05
      • 2017-12-16
      • 2011-03-21
      • 1970-01-01
      • 2013-04-07
      • 2022-01-05
      • 2017-11-20
      相关资源
      最近更新 更多