Crontab使用不正确版本的Python运行脚本



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

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

我有默认2.6附带的CentOS 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

更新Python脚本的shebang以显式使用Python 2.7:

#!/usr/bin/env python2.7

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

最新更新