The subprocess is killed even without nohup

I use subprocess.Popento run several processes.

The code looks something like this:

while flag > 0:
   flag = check_flag()
   c = MyClass(num_process=10)
   c.launch()

MyClassif 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 cases, a running subprocess dies, sometimes in the middle of a run. In some cases, it ends.

However, if I use subprocess.Popendirectly in the while loop, the process continues and terminates in a timely manner.

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

+4
source share
1 answer

nohup SIGHUP , . , SIGINT SIGTERM, , , . Popen preexec_fn.

:

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

.

:

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

All Articles