如何使用python中的commands模块在后台运行命令?

如何使用python中的commands模块在后台运行命令?,python,background-process,Python,Background Process,我想在后台使用python 2.7运行一个系统命令,这是我的: import commands path = '/fioverify.fio' cmd= "/usr/local/bin/fio" + path + " "+ " &" print cmd handle = commands.getstatusoutput(cmd) 这是失败的。如果我删除了符号和&,它就会工作。我需要在后台运行一个命令(/usr/local/bin/fio/fioverifypath) 有关如何完成此操

我想在后台使用python 2.7运行一个系统命令,这是我的:

import commands
path = '/fioverify.fio'

cmd= "/usr/local/bin/fio" + path + " "+ " &"
print cmd
handle = commands.getstatusoutput(cmd)
这是失败的。如果我删除了符号和
&
,它就会工作。我需要在后台运行一个命令(
/usr/local/bin/fio/fioverifypath


有关如何完成此操作的任何指针?

使用该模块,允许您将命令作为子进程(在后台)运行并检查其状态。

不要使用
命令
;它已被弃用,实际上对您的目的没有用处。改用
子流程

fio = subprocess.Popen(["/usr/local/bin/fio", path])
与进程并行运行
fio
命令,并将变量
fio
绑定到进程的句柄。然后,您可以调用
fio.wait()
等待进程完成并检索其返回状态。

您也可以尝试,它支持后台命令:

import sh

bin = sh.Command("/usr/local/bin/fio/fioverify.fio")
handle = bin(_bg=True)
# ... do other code ...
handle.wait()
使用Python 2.7在后台运行进程
命令。getstatusoutput(…)
不够智能,无法处理后台进程,请使用
subprocess.Popen
os.system

在后台进程上复制命令的方式错误。getstatusoutput
失败:

import commands
import subprocess

#This sleeps for 2 seconds, then stops, 
#commands.getstatus output handles sleep 2 in foreground okay
print(commands.getstatusoutput("sleep 2"))

#This sleeps for 2 seconds in background, this fails with error:
#sh: -c: line 0: syntax error near unexpected token `;'
print(commands.getstatusoutput("sleep 2 &"))
#subprocess handles the sleep 2 in foreground okay:
proc = subprocess.Popen(["sleep", "2"], stdout=subprocess.PIPE)
output = proc.communicate()[0]
print(output)

#alternate way subprocess handles the sleep 2 in foreground perfectly fine:
proc = subprocess.Popen(['/bin/sh', '-c', 'sleep 2'], stdout=subprocess.PIPE)
output = proc.communicate()[0]
print("we hung for 2 seconds in foreground, now we are done")
print(output)


#And subprocess handles the sleep 2 in background as well:
proc = subprocess.Popen(['/bin/sh', '-c', 'sleep 2'], stdout=subprocess.PIPE)
print("Broke out of the sleep 2, sleep 2 is in background now")
print("twiddling our thumbs while we wait.\n")
proc.wait()
print("Okay now sleep is done, resume shenanigans")
output = proc.communicate()[0]
print(output)
import os
#sleep 2 in the foreground with os.system works as expected
os.system("sleep 2")

import os
#sleep 2 in the background with os.system works as expected
os.system("sleep 2 &")
print("breaks out immediately, sleep 2 continuing on in background")
演示subprocess.Popen如何在后台进程上成功:

import commands
import subprocess

#This sleeps for 2 seconds, then stops, 
#commands.getstatus output handles sleep 2 in foreground okay
print(commands.getstatusoutput("sleep 2"))

#This sleeps for 2 seconds in background, this fails with error:
#sh: -c: line 0: syntax error near unexpected token `;'
print(commands.getstatusoutput("sleep 2 &"))
#subprocess handles the sleep 2 in foreground okay:
proc = subprocess.Popen(["sleep", "2"], stdout=subprocess.PIPE)
output = proc.communicate()[0]
print(output)

#alternate way subprocess handles the sleep 2 in foreground perfectly fine:
proc = subprocess.Popen(['/bin/sh', '-c', 'sleep 2'], stdout=subprocess.PIPE)
output = proc.communicate()[0]
print("we hung for 2 seconds in foreground, now we are done")
print(output)


#And subprocess handles the sleep 2 in background as well:
proc = subprocess.Popen(['/bin/sh', '-c', 'sleep 2'], stdout=subprocess.PIPE)
print("Broke out of the sleep 2, sleep 2 is in background now")
print("twiddling our thumbs while we wait.\n")
proc.wait()
print("Okay now sleep is done, resume shenanigans")
output = proc.communicate()[0]
print(output)
import os
#sleep 2 in the foreground with os.system works as expected
os.system("sleep 2")

import os
#sleep 2 in the background with os.system works as expected
os.system("sleep 2 &")
print("breaks out immediately, sleep 2 continuing on in background")
os.system如何处理后台进程的演示:

import commands
import subprocess

#This sleeps for 2 seconds, then stops, 
#commands.getstatus output handles sleep 2 in foreground okay
print(commands.getstatusoutput("sleep 2"))

#This sleeps for 2 seconds in background, this fails with error:
#sh: -c: line 0: syntax error near unexpected token `;'
print(commands.getstatusoutput("sleep 2 &"))
#subprocess handles the sleep 2 in foreground okay:
proc = subprocess.Popen(["sleep", "2"], stdout=subprocess.PIPE)
output = proc.communicate()[0]
print(output)

#alternate way subprocess handles the sleep 2 in foreground perfectly fine:
proc = subprocess.Popen(['/bin/sh', '-c', 'sleep 2'], stdout=subprocess.PIPE)
output = proc.communicate()[0]
print("we hung for 2 seconds in foreground, now we are done")
print(output)


#And subprocess handles the sleep 2 in background as well:
proc = subprocess.Popen(['/bin/sh', '-c', 'sleep 2'], stdout=subprocess.PIPE)
print("Broke out of the sleep 2, sleep 2 is in background now")
print("twiddling our thumbs while we wait.\n")
proc.wait()
print("Okay now sleep is done, resume shenanigans")
output = proc.communicate()[0]
print(output)
import os
#sleep 2 in the foreground with os.system works as expected
os.system("sleep 2")

import os
#sleep 2 in the background with os.system works as expected
os.system("sleep 2 &")
print("breaks out immediately, sleep 2 continuing on in background")

在这种情况下,“在后台运行”是什么意思?脚本完成后继续吗?与脚本一起运行?同样值得注意的是,它被弃用以支持。我的要求是:我希望我的脚本不要等到fio执行完成。脚本应在FIO在后台运行时继续。我还想取fio分叉的pid。谢谢。知道了。handle=subprocess.Popen([“/usr/local/bin/fio”,path])在后台运行它。你上面所说的工作,所以现在不需要代码剪贴。我后来作了相应的答复。