Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crontab using incorrect version of Python to run script

crontab is using version 2.6 to run a script that requires 2.7 to run. How do I set the default version of Python to be 2.7 permanently? running ./file.py works fine, its just when its run through 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

edit issue resolved

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
like image 685
NeonLights86 Avatar asked Jun 27 '15 19:06

NeonLights86


2 Answers

If you have a separate version of python installed for 2.7 you can look for it with whereis python

I have CentOS 6 which comes with 2.6 by default, so this command returns:

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

Of these /usr/local/bin/python2.7 is the one I'm interested in, so when I put a job in crontab, I choose it explicitly:

30 00 * * * /usr/local/bin/python2.7 /home/mike/job.py
like image 115
Mike Avatar answered Oct 23 '22 18:10

Mike


Update the shebang of your Python script to use Python 2.7 explicitly:

#!/usr/bin/env python2.7

This first line tells Unix which interpreter to use.

like image 32
Finwood Avatar answered Oct 23 '22 16:10

Finwood