crontab 不会运行 python 脚本

编程入门 行业动态 更新时间:2024-10-27 12:25:15
本文介绍了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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-27 04:48:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/679109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   crontab   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!