login community faq

I have a Python 2.7 script,test.py, that I wish to run via crontab. Its only function is to print the Python version number.

#!/usr/bin/env python2.7
import sys
print sys.version

It did not work, so I created an sh script named crontestscript to run it 5 ways. First using the default Python, then versions 2.5, 2.6, 2.7, and lastly the 2.7 version specified within the test.py script.

#!/bin/sh
echo "crontestscript starting =====" >> $HOME/cron/crontest.log
date +"%Y-%m-%d %T" >> $HOME/cron/crontest.log
python $HOME/cron/test.py >> $HOME/cron/crontest.log
python2.5 $HOME/cron/test.py >> $HOME/cron/crontest.log
python2.6 $HOME/cron/test.py >> $HOME/cron/crontest.log
python2.7 $HOME/cron/test.py >> $HOME/cron/crontest.log
$HOME/cron/test.py >> $HOME/cron/crontest.log

I ran the script via the command line and set crontab to run the same script 45 minutes after the hour. The results are below. Only Python 2.4.3 runs via crontab.

crontestscript starting =====
2011-07-31 11:41:28
2.4.3 (#1, May  5 2011, 16:39:09) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)]
2.5.4 (r254:67916, Nov 22 2010, 23:13:26) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)]
2.6.5 (r265:79063, Nov 23 2010, 02:02:03) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)]
2.7.1 (r271:86832, Dec  1 2010, 06:29:57) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)]
2.7.1 (r271:86832, Dec  1 2010, 06:29:57) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)]
crontestscript starting =====
2011-07-31 11:45:01
2.4.3 (#1, May  5 2011, 16:39:09) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)]

Is there way to access Python 2.7 via crontab?

asked Jul 31 '11 at 12:01

crosseyedpenguin's gravatar image

crosseyedpenguin
113


The reason "/usr/bin/env python2.7" does not work is because cron uses a different environment, and /usr/local/bin is not on the path in that environment.

The fact that cron uses a different environment from your normal shell account is mentioned in our own documentation and elsewhere on the web, eg:

You can use /usr/local/bin/python2.7 to run Python 2.7 via your cron jobs.

Another solution would be to set your PATH environment variable at the top of your crontab, eg:

1
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/username/bin:$PATH

Hope that helps!

answered Jul 31 '11 at 12:27

seanf's gravatar image

seanf ♦♦
56541220

Thanks, /usr/local/bin/python2.7 works for me.

(Jul 31 '11 at 15:14) crosseyedpenguin crosseyedpenguin's gravatar image
Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×229
×50
×37
×21

Asked: Jul 31 '11 at 12:01

Seen: 1,148 times

Last updated: Jul 31 '11 at 15:14

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited