subprocess gets killed even with nohup

梦想与她 提交于 2021-02-07 03:18:33

问题


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?


回答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)


来源:https://stackoverflow.com/questions/37118991/subprocess-gets-killed-even-with-nohup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!