我有以下代码,它试图在Linux中启动下面的每个“命令”。如果这两个命令中的任何一个因任何原因崩溃,模块都会尝试让这两个命令保持运行。
#!/usr/bin/env python
import subprocess
commands = [ ["screen -dmS RealmD top"], ["screen -DmS RealmD top -d 5"] ]
programs = [ subprocess.Popen(c) for c in commands ]
while True:
for i in range(len(programs)):
if programs[i].returncode is None:
continue # still running
else:
# restart this one
programs[i]= subprocess.Popen(commands[i])
time.sleep(1.0)在执行代码时,会抛出以下异常:
Traceback (most recent call last):
File "./marp.py", line 82, in <module>
programs = [ subprocess.Popen(c) for c in commands ]
File "/usr/lib/python2.6/subprocess.py", line 595, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1092, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory我想我遗漏了一些明显的东西,有人能看到上面的代码有什么问题吗?
发布于 2009-05-23 17:31:21
使用["screen", "-dmS", "RealmD", "top"]而不是["screen -dmS RealmD top"]。
也可以使用screen的完整路径。
发布于 2009-05-23 17:29:52
唯一的猜测是它找不到screen。试试/usr/bin/screen或which screen给你的任何东西。
发布于 2013-03-08 06:05:54
问题是您的命令应该被拆分。子过程要求cmd是一个列表,而不是一个字符串。它不应该是:
subprocess.call('''awk 'BEGIN {FS="\t";OFS="\n"} {a[$1]=a [$1] OFS $2 FS $3 FS $4} END
{for (i in a) {print i a[i]}}' 2_lcsorted.txt > 2_locus_2.txt''') 那行不通的。如果您为subprocess提供一个字符串,它会假定这是您想要执行的命令的路径。该命令需要是一个列表。查看http://www.gossamer-threads.com/lists/python/python/724330。另外,因为您使用的是文件重定向,所以应该使用subprocess.call(cmd, shell=True)。您也可以使用shlex。
https://stackoverflow.com/questions/901982
复制相似问题