题
我有以下代码试图在Linux中启动每个“命令”的下方。该模块试图保持每个运行2个命令如果任应崩溃无论出于何种原因。
#!/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
我觉得我失去了一些东西很明显,任何人都可以看到什么毛病上面的代码?
解决方案
使用["screen", "-dmS", "RealmD", "top"]
代替["screen -dmS RealmD top"]
。
也许还使用的完整路径screen
。
其他提示
唯一的猜测是,它无法找到screen
。尝试/usr/bin/screen
或任何which screen
给你。
的问题是,你的命令应该被拆分。 subprocces要求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''')
这将无法工作。如果你喂子字符串,它会假设是路径要执行的命令。该命令需要一个列表。请查看 http://www.gossamer-threads.com/lists/python/蟒蛇/ 724330 。此外,因为您使用文件重定向,你应该使用subprocess.call(cmd, shell=True)
。您还可以使用shlex
。
commands = [ "screen -dmS RealmD top", "screen -DmS RealmD top -d 5" ]
programs = [ subprocess.Popen(c.split()) for c in commands ]
我得到同样的错误时写这样的: -
subprocess.Popen("ls" ,shell = False , stdout = subprocess.PIPE ,stderr = subprocess.PIPE)
和当我提出问题得以解决的壳=真强>。它将工作
subprocess.Popen("ls" ,shell = False , stdout = subprocess.PIPE ,stderr = subprocess.PIPE, shell=True)
以防万一。我也卡住了,出现此错误和问题是,我的文件是在DOS,而不是UNIX因此在:
return subprocess.call(lst_exp)
其中lst_exp是ARGS的列表,它们中的一个被“未找到”,因为它是在DOS代替UNIX但抛出误差是相同的:
File "/var/www/run_verifier.py", line 59, in main
return subprocess.call(lst_exp)
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
不隶属于 StackOverflow