Python 在终止父进程时不终止运行子进程
我需要运行这个子进程,但我希望它在后台运行,如果主进程死了,不要杀死它。我该怎么做?如何在终端上无回声的情况下进行 就目前而言,这是可行的,但它不会继续使用python脚本的其余部分 …更新。。。 我试着 但是日志文件显示Python 在终止父进程时不终止运行子进程,python,subprocess,mpi,child-process,Python,Subprocess,Mpi,Child Process,我需要运行这个子进程,但我希望它在后台运行,如果主进程死了,不要杀死它。我该怎么做?如何在终端上无回声的情况下进行 就目前而言,这是可行的,但它不会继续使用python脚本的其余部分 …更新。。。 我试着 但是日志文件显示 subprocess.Popen(['nohup', cmd], stdout=open('/dev/null', 'w'), stderr=open('log.log', 'a'), preexe
subprocess.Popen(['nohup', cmd],
stdout=open('/dev/null', 'w'),
stderr=open('log.log', 'a'),
preexec_fn=os.setpgrp
)
并且命令在
nohup: failed to run command ‘/data/ParaView/bin/mpiexec -np 1
/data/ParaView/bin/pvserver --server-port=11111 -display :0.0 --force-
offscreen-rendering ’: No such file or directory
为什么nohup会失败
感谢@abarnert,修复:
你在找吗?可能重复的@abarnert是的,我尝试了,
subprocess.Popen(['nohup',cmd],stdout=open('/dev/null','w'),stderr=open('log.log','a'),preexec_fn=os.setpgrp)
…它在后台工作,但失败“nohup:无法运行命令'/data/ParaView/bin/mpiexec-np 1/data/ParaView/bin/pvserver--服务器端口=11111-显示:0.0--强制屏幕外渲染”:没有这样的文件或目录" . 当使用subprocess.call和shell=True运行时,命令可以正常工作,但是使用Popen时,会在日志文件中显示此错误@雅罗斯拉夫苏日科夫你把不同的东西混在一起了。如果要使用shell=True
,必须构建单个命令行字符串,包括开头的nohup
。如果不想使用shell=True
,则必须构建一个包含所有独立参数的列表。如果你在中间做了一些事情,比如['nohup',cmd]
,你将把整个cmd
字符串作为一个巨大的参数传递,你希望nohup
运行的程序的名称,当然没有这样的程序。/@abarnert谢谢,它与你的帮助一起工作,非常感谢你。
nohup: failed to run command ‘/data/ParaView/bin/mpiexec -np 1
/data/ParaView/bin/pvserver --server-port=11111 -display :0.0 --force-
offscreen-rendering ’: No such file or directory
subprocess.call(cmd, shell=True)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
np_mpi = "1"
mpiexec = "/data/ParaView/bin/mpiexec"
pvserver = " /data/ParaView/bin/pvserver "
s_port="--server-port=11111"
cmd = ['nohup', mpiexec, "-np", np_mpi, pvserver,
s_port, "-display", ":0.0", "--force-offscreen-rendering"
]
subprocess.Popen(cmd,
stdout=open('/dev/null', 'w'),
stderr=open('log.log', 'a'),
preexec_fn=os.setpgrp
)