杀死父级时运行subprocess而不杀死

时间:2018-05-03 22:09:52

标签: python subprocess mpi child-process

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import subprocess

np_mpi = "1"
s_port = "11111"

cmd = "/data/ParaView/bin/mpiexec" + " -np " + \
    np_mpi + " /data/ParaView/bin/pvserver " + \
    "--server-port=" + \
    s_port + " -display " + " :0.0 " + \
    " --force-offscreen-rendering "

subprocess.call(cmd, shell=True)

我需要运行这个子进程,但是我希望在后台运行,而不是在主进程死掉时终止它。我该怎么做?怎么做没有终端回声?

目前这有效,但是,不会继续使用脚本python的其余部分......

... ...更新   我试试

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

和命令

 subprocess.call(cmd, shell=True)

为什么nohup会失败?

1 个答案:

答案 0 :(得分:0)

  

感谢@abarnert,修复:

 #!/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
                  )