0
#!/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?

al3x609
  • 173
  • 1
  • 10
  • Are you looking for [`nohup`](https://en.wikipedia.org/wiki/Nohup)? – abarnert May 03 '18 at 22:19
  • Possible duplicate of [Python spawn off a child subprocess, detach, and exit](https://stackoverflow.com/questions/5772873/python-spawn-off-a-child-subprocess-detach-and-exit) – Yaroslav Surzhikov May 03 '18 at 22:21
  • @abarnert yes, I try with , `subprocess.Popen(['nohup', cmd], stdout=open('/dev/null', 'w'), stderr=open('log.log', 'a'), preexec_fn=os.setpgrp )` ....It work for background, but fail "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" . when run with subprocess.call and shell=True, the command work fine, but, with Popen show this error on log file. @YaroslavSurzhikov – al3x609 May 03 '18 at 22:52
  • 1
    You're mixing up different things here. If you want to use `shell=True`, you have to build a single command-line string, including the `nohup ` at the start. If you don't want to use `shell=True`, you have to build a list of all of the separate arguments. If you do something halfway between, like `['nohup', cmd]`, you're passing that entire `cmd` string as one giant argument, the name of the program you want `nohup` to run, and of course there is no such program./ – abarnert May 03 '18 at 23:14
  • @abarnert thanks, it works with your help, thank you so much. – al3x609 May 04 '18 at 00:06

1 Answers1

0

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
                  )
al3x609
  • 173
  • 1
  • 10