2015-06-27 161 views
2

crontab使用2.6版運行需要運行2.7的腳本。如何將Python的默認版本永久設置爲2.7? 運行./file.py正常工作,它只是當它通過運行crontabCrontab使用不正確的Python版本運行腳本

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 
+0

你的環境是否安裝了python 2.7? – Pradheep

+0

是的,我可以運行./file.py罰款只是當它運行通過crontab它的問題 – NeonLights86

+0

其中python2.7安裝? –

回答

3

如果您已經安裝了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 
+0

完整路徑修復它,謝謝。 – NeonLights86

4

更新您的Python腳本的shebang使用Python 2.7明確:

#!/usr/bin/env python2.7 

第一行告訴Unix要使用哪個解釋器。