简体   繁体   中英

Run subprocess without kill when kill parent

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

I need run this subprocess, but I want that run in the background, and not kill it if main process dies. How do I do it? How to do it without echo on terminal?

For the moment this works but, no it continue with the rest of script python...

...update... I try

subprocess.Popen(['nohup', cmd],
             stdout=open('/dev/null', 'w'),
             stderr=open('log.log', 'a'),
             preexec_fn=os.setpgrp
             ) 

but the log file shows

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

and the command its ok with

 subprocess.call(cmd, shell=True)

Why does nohup fail?

thanks to @abarnert, the fix:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM