2

I want my python job to start another process and exit, while keeping the child running.

When I start a subprocess using

Popen([sys.executable,"foo","bar"])

(as recommended instead of spawn), the child dies when the parent exits (the above Popen is about the last thing the parent does).

This, of course, can be remedied using nohup:

with open("/dev/null","w") as fd:
    Popen(["nohup",sys.executable,"foo","bar"],stdout=fd)

(I have to redirect output to /dev/null to prevent nohup from creating file nohup.out and announcing that misdeed on stdout).

Is there a more elegant/pythonic way to deal with this?

I also tried os.fork + signal.signal(signal.SIGHUP,signal.SIG_IGN) and the child still died when the parent exited.

sds
  • 58,617
  • 29
  • 161
  • 278
  • Possible duplicate of [subprocess.wait() not waiting for Popen process to finish (when using threads)?](http://stackoverflow.com/questions/6341358/subprocess-wait-not-waiting-for-popen-process-to-finish-when-using-threads) – Emin Mastizada Apr 05 '17 at 21:47
  • How do you produce the situation that "the parent dies" when you test? Depending on this you might hunt ghosts because something actively kills your child along with the parent, so not the death of the parent is the reason for your child to perish. – Alfe Apr 05 '17 at 21:54
  • @Alfe: the parent exits on its own, no one does any killing here – sds Apr 05 '17 at 22:15
  • @EminMastizada: my question is almost completely unrelated. No threads, no wait(). – sds Apr 05 '17 at 22:15
  • Then you have a strange setup I cannot reproduce. I start an interactive Python session and type: `from subprocess import Popen`, `p = Popen([ 'bash', '-c', 'while sleep 3; do date; done' ])`, then I exit the session (Ctrl-D). The child process continues to print dates into my terminal. Or using python in the child: `Popen([ sys.executable, '-c', 'import time\nwhile True:\n time.sleep(3.0)\n print time.time()\n' ])` – Alfe Apr 05 '17 at 22:22
  • Can the termination of th father have any side-effect (e. g. like a closing terminal window, a broken pipe, or similar) which in turn causes the child to terminate? – Alfe Apr 05 '17 at 22:28
  • 1
    @Alfe: you are right: I was testing under emacs where (apparently) parent existing cause tty closure; I tried bash and everything is fine now – sds Apr 05 '17 at 22:31
  • @Alfe: my deployment environment will be cron, I suspect that it will be similar to emacs – sds Apr 05 '17 at 22:33
  • 2
    Most dramatic on-topic title on SO? – HFBrowning Apr 05 '17 at 22:42
  • 1
    @HFBrowning ;-) http://sds.podval.org/zombie.html – sds Apr 05 '17 at 22:49

0 Answers0