2

I'm running into a problem using the subprocess module to open completely independent processes through Python. I need the process spawned by the python script to live on even if the python program dies. I've noticed that subprocess creates a child process and nohup is not keeping it alive. It needs to work both on Windows and Linux, so I'm not sure if os.fork will work.

The structure I am aiming for is a main program running at all times that calls other programs, and if the main program dies I want the spawned programs to live on.

Main program

import subprocess
import time

subprocess.Popen(["/usr/bin/python", "/home/jchoinski/Desktop/Sentinel1.1/programTest.py"], stdin=subprocess.PIPE, stdout=PIPE, stderr=PIPE)
while True:
   time.sleep(1)

Spawned Program

import time

l = 1
while l < 100000:
    l += 1
    print(l)
    time.sleep(1)

The process tree looks like this:

MainProgram
|_CalledProgram

and I want it to look like:

MainProgram
CalledProgram

Any ideas? Should I even use subprocess for this?

Juliuszc
  • 377
  • 2
  • 4
  • 16
  • 1
    http://stackoverflow.com/questions/8947711/call-a-python-subprocess-as-daemon-and-exit – Huazuo Gao Sep 23 '15 at 14:40
  • 1
    http://stackoverflow.com/questions/13592219/launch-a-totally-independent-process-from-python?rq=1 – Psytho Sep 23 '15 at 14:43
  • 1
    The first link does not spawn an independent process and the second link does not work for *nix systems. – Juliuszc Sep 23 '15 at 15:10
  • Not sure how to do it platform independent, but https://en.wikipedia.org/wiki/Nohup and this http://stackoverflow.com/questions/285015/linux-prevent-a-background-process-from-being-stopped-after-closing-ssh-client have some general clues. – Robert E Sep 23 '15 at 15:18
  • don't use `PIPE` unless you read from the pipes. [Use `subprocess.DEVNULL` instead](http://stackoverflow.com/a/11270665/4279). Also, use `close_fds=True` explicitly (Python 2 requires it). It should also fix "nohup is not keeping it alive" issue. – jfs Sep 23 '15 at 15:32
  • I've managed to get the program to keep running on *nix system with nohup, but that option is not available for Windows. I guess I'll just have to check for os type and handle the cases accordingly. – Juliuszc Sep 23 '15 at 15:36

2 Answers2

0

I couldn't find a true universal way to start the program independently as it's own process. I managed to get what I want by checking to see if it is a *nix OS and appending 'nohup' to the beginning if true. Windows does not kill child processes so it worked out.

Juliuszc
  • 377
  • 2
  • 4
  • 16
  • [`nohup` is not enough](http://stackoverflow.com/q/6011235/4279), you might need `preexec_fn=os.setpgrp` too (and perhaps [other points from Unix daemon list](https://www.python.org/dev/peps/pep-3143/#correct-daemon-behaviour)). There are no process trees on Windows. There are "Job Objects" on Windows and [the default behavior is not to kill child processes](http://stackoverflow.com/q/53208/4279) – jfs Sep 24 '15 at 17:54
  • here's [code example that shows how to emulate `nohup` in pure Python](http://stackoverflow.com/a/27549535/4279) – jfs Sep 24 '15 at 17:57
-1

Use Multiprocessing library instead and run the process as daemon.

hspandher
  • 15,934
  • 2
  • 32
  • 45
  • 3
    it is the opposite of what OP wants. If the main process exits then all its daemonic child processes are killed. Note: [daemon process in `multiprocessing`](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process.daemon) has a _different_ meaning from [Unix daemon](https://www.python.org/dev/peps/pep-3143/#correct-daemon-behaviour) – jfs Sep 23 '15 at 20:58