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 31 Jul '11, 12:01

crosseyedpenguin's gravatar image

crosseyedpen...
1114
accept rate: 0%


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:

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

Hope that helps!

link

answered 31 Jul '11, 12:27

seanf's gravatar image

seanf ♦♦
8.0k1726
accept rate: 39%

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

(31 Jul '11, 15:14) crosseyedpen... crosseyedpenguin's gravatar image
Your answer
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

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×275
×62
×41
×34

Asked: 31 Jul '11, 12:01

Seen: 1,623 times

Last updated: 31 Jul '11, 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