crontab 不会运行 python 脚本 [英] Crontab wont run python script

查看:29
本文介绍了crontab 不会运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从命令行执行我的 python 脚本时,我没有像这样的问题:

When I execute my python script from the command line I have no problems like so:

[rv@med240-183 db]$ python formatdb.py
[rv@med240-183 分贝]$

[rv@med240-183 db]$ python formatdb.py
[rv@med240-183 db]$

当我每半夜尝试使用 crontab 运行脚本时,我收到一系列错误:

When I try to use crontab to run the script every midnight I get a series of errors:

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')'

我的脚本目录如下:

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

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

Crontab 看起来像:

Crontab looks like:

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

我的python脚本看起来像:

and my python script looks like:

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()+'
')
    #f.write(i[1].strip().replace(' ','')+'
')
    s = re.sub(r'[^w]','',str(i[1]))
    s = ''.join(s)
    for k in range(0, len(s), 60):
        f.write('%s
' % (s[k:k+60]))
    f.write('
')

f.close

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

推荐答案

添加

#!/usr/bin/env python

到脚本的开头 - 现在它正在尝试将脚本作为 bash 执行,该行显示我是 python 脚本,请使用正确的解释器".它也称为 hash-bang 行,但它必须是脚本中的第一行.

to the beginning of your script - right now it's trying to execute your script as a bash, that line says "I'm a python script, please use the right interpreter". It's also called a hash-bang line, but it needs to be the first line in your script.

这篇关于crontab 不会运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆