# Python and Crontab

**URL:** <https://community.unix.com/t/python-and-crontab/170153>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [March 28, 2007, 3:58pm UTC](https://community.unix.com/t/python-and-crontab/170153 "2007-03-28T15:58:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidfrank](https://community.unix.com/letter_avatar/davidfrank/32/5_5575768a8748004e209b776fc1b2916d.png) [@davidfrank](https://community.unix.com/u/davidfrank)\
**Post date:** [March 28, 2007, 3:58pm UTC](https://community.unix.com/t/python-and-crontab/170153/1 "2007-03-28T15:58:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gopidesaboyina](https://community.unix.com/letter_avatar/gopidesaboyina/32/5_5575768a8748004e209b776fc1b2916d.png) [@gopidesaboyina](https://community.unix.com/u/gopidesaboyina)\
**Post date:** [March 28, 2007, 4:30pm UTC](https://community.unix.com/t/python-and-crontab/170153/2 "2007-03-28T16:30:40Z")

</div>

were you able to run that script from terminal manually ?

---

<div class="post-metadata">

**Author:** ![davidfrank](https://community.unix.com/letter_avatar/davidfrank/32/5_5575768a8748004e209b776fc1b2916d.png) [@davidfrank](https://community.unix.com/u/davidfrank)\
**Post date:** [March 28, 2007, 5:44pm UTC](https://community.unix.com/t/python-and-crontab/170153/3 "2007-03-28T17:44:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidfrank](https://community.unix.com/letter_avatar/davidfrank/32/5_5575768a8748004e209b776fc1b2916d.png) [@davidfrank](https://community.unix.com/u/davidfrank)\
**Post date:** [March 28, 2007, 6:30pm UTC](https://community.unix.com/t/python-and-crontab/170153/4 "2007-03-28T18:30:05Z")

</div>

Hey,

I've found the problem. I'm an idiot 😃

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

---

<div class="post-metadata">

**Author:** ![Yogesh\_Sawant](https://community.unix.com/user_avatar/community.unix.com/yogesh_sawant/32/22562_2.png) [@Yogesh\_Sawant](https://community.unix.com/u/Yogesh_Sawant)\
**Post date:** [March 29, 2007, 3:47am UTC](https://community.unix.com/t/python-and-crontab/170153/5 "2007-03-29T03:47:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidfrank](https://community.unix.com/letter_avatar/davidfrank/32/5_5575768a8748004e209b776fc1b2916d.png) [@davidfrank](https://community.unix.com/u/davidfrank)\
**Post date:** [March 29, 2007, 12:33pm UTC](https://community.unix.com/t/python-and-crontab/170153/6 "2007-03-29T12:33:18Z")

</div>

Thanks for the info, Yashwant.

This forum is awesome!!

David

---

<div class="post-metadata">

**Author:** ![davidfrank](https://community.unix.com/letter_avatar/davidfrank/32/5_5575768a8748004e209b776fc1b2916d.png) [@davidfrank](https://community.unix.com/u/davidfrank)\
**Post date:** [March 29, 2007, 12:34pm UTC](https://community.unix.com/t/python-and-crontab/170153/7 "2007-03-29T12:34:22Z")

</div>

I'm sorry.....I meant Sawant. 🙂

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [March 29, 2007, 1:22pm UTC](https://community.unix.com/t/python-and-crontab/170153/8 "2007-03-29T13:22:47Z")

</div>

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