• <legend id='PPwnC'><style id='PPwnC'><dir id='PPwnC'><q id='PPwnC'></q></dir></style></legend>
    <i id='PPwnC'><tr id='PPwnC'><dt id='PPwnC'><q id='PPwnC'><span id='PPwnC'><b id='PPwnC'><form id='PPwnC'><ins id='PPwnC'></ins><ul id='PPwnC'></ul><sub id='PPwnC'></sub></form><legend id='PPwnC'></legend><bdo id='PPwnC'><pre id='PPwnC'><center id='PPwnC'></center></pre></bdo></b><th id='PPwnC'></th></span></q></dt></tr></i><div id='PPwnC'><tfoot id='PPwnC'></tfoot><dl id='PPwnC'><fieldset id='PPwnC'></fieldset></dl></div>

      • <bdo id='PPwnC'></bdo><ul id='PPwnC'></ul>

        <tfoot id='PPwnC'></tfoot>
      1. <small id='PPwnC'></small><noframes id='PPwnC'>

        从python运行程序,并在脚本被杀死后继续运行

        Run a program from python, and have it continue to run after the script is killed(从python运行程序,并在脚本被杀死后继续运行)
        <legend id='YNnn3'><style id='YNnn3'><dir id='YNnn3'><q id='YNnn3'></q></dir></style></legend>

          <bdo id='YNnn3'></bdo><ul id='YNnn3'></ul>

              <tbody id='YNnn3'></tbody>

            <small id='YNnn3'></small><noframes id='YNnn3'>

              1. <i id='YNnn3'><tr id='YNnn3'><dt id='YNnn3'><q id='YNnn3'><span id='YNnn3'><b id='YNnn3'><form id='YNnn3'><ins id='YNnn3'></ins><ul id='YNnn3'></ul><sub id='YNnn3'></sub></form><legend id='YNnn3'></legend><bdo id='YNnn3'><pre id='YNnn3'><center id='YNnn3'></center></pre></bdo></b><th id='YNnn3'></th></span></q></dt></tr></i><div id='YNnn3'><tfoot id='YNnn3'></tfoot><dl id='YNnn3'><fieldset id='YNnn3'></fieldset></dl></div>
                • <tfoot id='YNnn3'></tfoot>
                  本文介绍了从python运行程序,并在脚本被杀死后继续运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试过这样运行:

                  subprocess.Popen(['nohup', 'my_command'],
                                   stdout=open('/dev/null', 'w'),
                                   stderr=open('logfile.log', 'a'))
                  

                  如果父脚本正常退出,这会起作用,但如果我终止脚本 (Ctrl-C),我的所有子进程也会被终止.有没有办法避免这种情况?

                  This works if the parent script exits gracefully, but if I kill the script (Ctrl-C), all my child processes are killed too. Is there a way to avoid this?

                  我关心的平台是 OS X 和 Linux,使用 Python 2.6 Python 2.7.

                  The platforms I care about are OS X and Linux, using Python 2.6 and Python 2.7.

                  推荐答案

                  在 Unix 系统上执行此操作的通常方法是,如果您是父级,则分叉并退出.看看 os.fork() .

                  The usual way to do this on Unix systems is to fork and exit if you're the parent. Have a look at os.fork() .

                  这是一个完成这项工作的函数:

                  Here's a function that does the job:

                  def spawnDaemon(func):
                      # do the UNIX double-fork magic, see Stevens' "Advanced 
                      # Programming in the UNIX Environment" for details (ISBN 0201563177)
                      try: 
                          pid = os.fork() 
                          if pid > 0:
                              # parent process, return and keep running
                              return
                      except OSError, e:
                          print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror) 
                          sys.exit(1)
                  
                      os.setsid()
                  
                      # do second fork
                      try: 
                          pid = os.fork() 
                          if pid > 0:
                              # exit from second parent
                              sys.exit(0) 
                      except OSError, e: 
                          print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror) 
                          sys.exit(1)
                  
                      # do stuff
                      func()
                  
                      # all done
                      os._exit(os.EX_OK)
                  

                  这篇关于从python运行程序,并在脚本被杀死后继续运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                  <tfoot id='MPNAQ'></tfoot>

                  <small id='MPNAQ'></small><noframes id='MPNAQ'>

                      <legend id='MPNAQ'><style id='MPNAQ'><dir id='MPNAQ'><q id='MPNAQ'></q></dir></style></legend>
                        <tbody id='MPNAQ'></tbody>

                          • <bdo id='MPNAQ'></bdo><ul id='MPNAQ'></ul>

                            <i id='MPNAQ'><tr id='MPNAQ'><dt id='MPNAQ'><q id='MPNAQ'><span id='MPNAQ'><b id='MPNAQ'><form id='MPNAQ'><ins id='MPNAQ'></ins><ul id='MPNAQ'></ul><sub id='MPNAQ'></sub></form><legend id='MPNAQ'></legend><bdo id='MPNAQ'><pre id='MPNAQ'><center id='MPNAQ'></center></pre></bdo></b><th id='MPNAQ'></th></span></q></dt></tr></i><div id='MPNAQ'><tfoot id='MPNAQ'></tfoot><dl id='MPNAQ'><fieldset id='MPNAQ'></fieldset></dl></div>