Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()

MyClass if something like the following:

MyClass(object)
   def __init__(self, num_process):
      self.num_process = num_process

   def launch(self):
      if self.check_something() < 10:
         for i in range(self.num_process):
             self.launch_subprocess()

   def launch_subprocess(self):
      subprocess.Popen(["nohup",
                       "python",
                       "/home/mypythonfile.py"],
                       stdout=open('/dev/null', 'w'),
                       stderr=open('logfile.log', 'w'),
                       shell=False)

In most of the cases, the launched subprocess dies, sometimes in the middle of the run. In some cases, it completes.

However, if I use subprocess.Popen directly in the while loop, the process continues and finished timely.

Could someone tell me how can I get the processes to run in the background by using subprocess in the way as I described above?

like image 284
Abhishek Thakur Avatar asked May 09 '16 14:05

Abhishek Thakur


1 Answers

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)
like image 57
gdlmx Avatar answered Sep 19 '22 16:09

gdlmx