Python and Crontab

Hi,

I am running a Python program using crontab as follows:

          • /home/usernine/metriculate.py

But I keep getting an error: import: unable to open X server

The following is what was in my mail on execution of crontab:

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/usernine>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=usernine>
X-Cron-Env: <USER=usernine>
Message-Id: <20070328185601.8620D9B57C@dunlo.ancpeet.org>
Date: Wed, 28 Mar 2007 11:56:01 -0700 (PDT)

import: unable to open X server `'.
/home/usernine/metriculate.py: line 2: syntax error near unexpected token `('
/home/usernine/metriculate.py: line 2: `db1 = MySQLdb.connect(host="localhost", user="univer", passwd="loontoon", db="met")

I have no clue on what I should do about this error. An online search said that the error could be because of the DISPLAY environment and an xauth file. If that IS the problem please do tell me what I should do to solve this problem.

Thank you,

David

were you able to run that script from terminal manually ?

Yes, the Python script works perfectly when I run it using,

$ python metriculate.py

The script is a simple MySQL operation. I have tried several Python scripts with these MySQL operations, but all of them give the SAME error when I use them with crontab.

David

Hey,

I've found the problem. I'm an idiot :smiley:

It is required to add the line,

#!/usr/bin/python

at the beginning of the python program file. Though this is just a remark and does not cause any effect when the code is run manually, this IS required to be present when the same code is run through crontab.

Thanks,

David

what you think is just a remark is also an instruction to your shell (which is working as an interpreter) that what follows in the file is a script which should now be run using python and pick this python program at /usr/bin/python

what is meaningless to you might have meaning for the machine

Thanks for the info, Yashwant.

This forum is awesome!!

David

I'm sorry.....I meant Sawant. :slight_smile:

See [thread=7077]What does "#! /usr/bin/ksh" mean?[/thread]