【问题标题】:Python: File works on the command line but not with crontabPython:文件在命令行上工作,但不适用于 crontab
【发布时间】:2010-08-12 01:06:36
【问题描述】:

所以我有一个看起来像这样的文件:

#!/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”但也没有这样做。

【问题讨论】:

    标签: python crontab


    【解决方案1】:

    我怀疑它在您的子进程调用中抱怨“formatdb”的路径。尝试将其更改为完整路径:

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

    【讨论】:

      【解决方案2】:

      cron 守护进程通常只提供非常有限的 PATH。要么在 crontab 中放置一个更完整的 PATH,要么在 Python 代码中使用完整的路径名。

      【讨论】:

      • 我已经将路径输入到crontab文件中,我会尝试在python文件中输入完整路径
      猜你喜欢
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 2011-03-28
      • 1970-01-01
      • 2013-08-31
      • 2018-07-10
      • 1970-01-01
      相关资源
      最近更新 更多