How to use macports Python in a cron job

cronpython

I have installed Python 2.7 through mac ports, and now want to have it used in a cron job. Unfortunatly, each time the cron job gets involved, the standard Python of my machine (an iBook G4 using Tiger) which is 2.3 is involved, making my job fail.

My PATH is (from the set command)

PATH=/opt/local/bin:/opt/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin

P

How can I have my cron job be invoked with my PATH instead of the faulty one ?

Best Answer

The result of the PATH variable is quite different in the cron world. Invoke the script with the full path to the python binary (e.g. /opt/local/bin/python2.7) and you should be good.

This can be examined by running env as cronjob and comparing it to the result of running env as yourself in Terminal:

user's cron env:

PATH=/usr/bin:/bin:/usr/sbin:/sbin
...
SHELL=/bin/bash
HOME=/Users/user
USER=user
PWD=/
...

compared to user's env:

PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
...
SHELL=/bin/bash
HOME=/Users/user
USER=user
PWD=/Users/user
...

Alternatively you may add the PATH to the crontab:

sudo nano /var/cron/tabs/user

In newer systems (probably ≥10.6):

sudo nano /private/var/at/tabs/user

Change

...

*       *       *       *       *       some command

to

...
PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

*       *       *       *       *       some command