Skip to content Skip to sidebar Skip to footer

Subprocess Gets Killed Even With Nohup

I'm using subprocess.Popen to launch several processes. The code is something like this: while flag > 0: flag = check_flag() c = MyClass(num_process=10) c.launch() MyC

Solution 1:

The nohup only stop the SIGHUP signal when your master process exits normally. For other signal like SIGINT or SIGTERM, the child process receives the same signal as your parent process because it's in the same process group. There're two methods using the Popen's preexec_fn argument.

Setting the child process group:

subprocess.Popen(['nohup', 'python', '/home/mypythonfile.py'],
                 stdout=open('/dev/null', 'w'),
                 stderr=open('logfile.log', 'a'),
                 preexec_fn=os.setpgrp )

More information goes in another post.

Making the subprocesses ignore those signals:

def preexec_function():
    signal.signal(signal.SIGINT, signal.SIG_IGN)
subprocess.Popen( ... , preexec_fn=preexec_function)

Post a Comment for "Subprocess Gets Killed Even With Nohup"