2010-07-26 22 views
10

Quando eseguo il mio script python da linea di comando non ho problemi in questo modo:Crontab wont eseguire script Python

[camper @ med240-183 db] $ python formatdb.py
[RV @ med240 -183 dB] $

Quando provo ad usare crontab per eseguire lo script ogni mezzanotte ho una serie di errori:

import: unable to open X server `' @ import.c/ImportImageCommand/367. 
/home/rv/ncbi-blast-2.2.23+/db/formatdb.py: line 2: from: command not found 
/home/rv/ncbi-blast-2.2.23+/db/formatdb.py: line 3: from: command not found 
import: unable to open X server `' @ import.c/ImportImageCommand/367. 
/home/rv/ncbi-blast-2.2.23+/db/formatdb.py: line 6: syntax error near 
unexpected token `(' 
/home/rv/ncbi-blast-2.2.23+/db/formatdb.py: line 6: `conx = MySQLdb.connect 
(user = 'root', passwd = '******', db = 'vaxijen_antigens')' 

la directory del mio script è la seguente:

/home/rv/ncbi-blast-2.2.23+/db/

Crontab assomiglia:

SHELL=/bin/bash 
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/python/:/home/rv/ncbi-blast-2.2.23+/database_backup:/home/rv/ncbi-blast-2.2.23+/db/ 
MAILTO="******" 
HOME=/ 

# For details see man 4 crontabs 

# Example of job definition: 
# .---------------- minute (0 - 59) 
# | .------------- hour (0 - 23) 
# | | .---------- day of month (1 - 31) 
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ... 
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 
# | | | | | 
# * * * * * command to be executed 
0 0 * * * root /home/rv/ncbi-blast-2.2.23+/database_backup/backup.py 
0 0 * * * root /home/rv/ncbi-blast-2.2.23+/db/formatdb.py 

e il mio script python appare come segue:

import MySQLdb 
from subprocess import call 
from subprocess import Popen 
import re 

conx = MySQLdb.connect (user = 'root', passwd = '******', db = 'vaxijen_antigens') 

cursor = conx.cursor() 
cursor.execute('select * from sequence') 
row = cursor.fetchall() 

f = open('vdatabase.fasta', 'w') 

for i in row: 
    f.write('>'+i[0].strip()+'\n') 
    #f.write(i[1].strip().replace(' ','')+'\n') 
    s = re.sub(r'[^\w]','',str(i[1])) 
    s = ''.join(s) 
    for k in range(0, len(s), 60): 
     f.write('%s\n' % (s[k:k+60])) 
    f.write('\n') 

f.close 

Popen(["formatdb", "-p", "T", "-i", "vdatabase.fasta"]).wait() 
+0

Che sistema operativo avete? Dove sono alcuni bug simili in gentoo, prova ad usare questa risposta per esempio per risolvere il tuo problema http://schwobeseggl.de/2009/08/04/gentoo-portage-python/. Il tuo problema è che quando lo stai eseguendo manualmente, lo avvii dall'ambiente X, ma cron funziona senza X server ... –

risposta

27

Aggiungere

#!/usr/bin/env python 

all'inizio del tuo script - in questo momento sta provando a eseguire il tuo script come una bash, quella riga dice "Sono uno script python, per favore usa l'interprete giusto". Viene anche chiamata linea hash-bang, ma deve essere la prima riga del tuo script.

+2

D'oh ho dimenticato le basi grazie per avermelo ricordato – Phil

+5

Mi è successo un paio di volte - e come te non potrei vedere l'ovvio, whoops>.

+0

Cose fantastiche - solo aggiungendo che la linea di shebang mi ha salvato un'intera giornata di pettinature. Grazie :) –

Problemi correlati