Python:命令行可以运行的文件在crontab中无法工作

3 投票
2 回答
1490 浏览
提问于 2025-04-16 02:38

我有一个文件,内容是这样的:

#!/usr/bin/python
import MySQLdb
import subprocess
from subprocess import call
import re

conx = MySQLdb.connect (user = 'root', passwd = '******', db = 'vaxijen_antigens')
cursor = conx.cursor()
cursor.execute('select * from sequence')
row = cursor.fetchall()

f = open('/home/rv/ncbi-blast-2.2.23+/db/vdatabase.fasta', 'w')

for i in row:
    f.write('>'+i[0].strip()+'\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()

subprocess.call(['formatdb', '-p', 'T', '-i', r'/home/rv/ncbi-blast-2.2.23+/db/vdatabase.fasta'])

这个文件在命令行中运行没有问题,但当我尝试用crontab运行时却出现了这个错误:

  File "/home/rv/ncbi-blast-2.2.23+/db/formatdb.py", line 29, in <module>
    subprocess.call(['formatdb', '-p', 'T', '-i',
r'/home/rv/ncbi-blast-2.2.23+/db/vdatabase.fasta'])
OSError: [Errno 2] No such file or directory

我不明白,那个文件确实在那个目录里,我已经检查了好几遍。我试着把文件路径转换成原始字符串,所以在路径前加了小写的“r”,但这样也没用。

2 个回答

3

cron守护进程通常只提供一个非常有限的路径。你可以在crontab里设置一个更完整的路径,或者在Python代码中使用完整的文件路径。

5

我猜它是在抱怨你在子进程调用中“formatdb”的路径。试着把它改成完整的路径:

subprocess.call(['/home/path/formatdb', ...])

撰写回答