• <small id='nfhEj'></small><noframes id='nfhEj'>

    <legend id='nfhEj'><style id='nfhEj'><dir id='nfhEj'><q id='nfhEj'></q></dir></style></legend>
  • <tfoot id='nfhEj'></tfoot>

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

        如何在 bash 或 python 中在 Linux 上生成分离的后台进程

        时间:2023-09-03
        <tfoot id='RPdQD'></tfoot>
          <legend id='RPdQD'><style id='RPdQD'><dir id='RPdQD'><q id='RPdQD'></q></dir></style></legend>
        • <i id='RPdQD'><tr id='RPdQD'><dt id='RPdQD'><q id='RPdQD'><span id='RPdQD'><b id='RPdQD'><form id='RPdQD'><ins id='RPdQD'></ins><ul id='RPdQD'></ul><sub id='RPdQD'></sub></form><legend id='RPdQD'></legend><bdo id='RPdQD'><pre id='RPdQD'><center id='RPdQD'></center></pre></bdo></b><th id='RPdQD'></th></span></q></dt></tr></i><div id='RPdQD'><tfoot id='RPdQD'></tfoot><dl id='RPdQD'><fieldset id='RPdQD'></fieldset></dl></div>

            <tbody id='RPdQD'></tbody>
              <bdo id='RPdQD'></bdo><ul id='RPdQD'></ul>

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

                1. 本文介绍了如何在 bash 或 python 中在 Linux 上生成分离的后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Linux 上有一个长时间运行的 python 脚本,在某些情况下它需要执行命令来停止和重新启动自身.所以,我想要一个外部脚本(在 bash 或 python 中)执行命令以重新启动原始脚本.让我详细说明.

                  I have a long running python script on Linux, and in some situations it needs to execute a command to stop and restart itself. So, I would like to have an external script (either in bash or python) that executes command to restart the original script. Let me elaborate.

                  假设我有 original_script.py.在 original_script.py 我有这个无限循环:

                  Suppose I have original_script.py. In original_script.py I have this in an infinite loop:

                  if some_error_condition:
                      somehow call external script external.sh or external.py
                  

                  假设我可以调用 external.sh 并且它包含以下内容:

                  Let's suppose I can call external.sh and it contains this:

                  #!/bin/bash
                  command_to_restart_original_script
                  

                  最后,我知道了命令command_to_restart_original_script".那不是问题.需要的是以某种方式调用外部脚本external.sh"的python命令.我需要外部脚本(这是一个子进程)在父进程 original_script.py 重新启动时继续运行,即我需要子进程被分离/守护.我该怎么做?

                  Finally, I know the command "command_to_restart_original_script". That isn't the problem. What need is the python command to "somehow call external script external.sh". I need the external script (which is a child process) to keep running as the parent process original_script.py is restarting, ie I need the child process to be detached/daemonized. How do I do this?

                  推荐答案

                  我在各个地方找到了很多建议,但唯一对我有用的答案是:如何在后台启动和运行外部脚本?

                  I found lots of suggestions in various places, but the only answer that worked for me was this: How to launch and run external script in background?

                  import subprocess
                  subprocess.Popen(["nohup", "python", "test.py"])
                  

                  在我的例子中,我运行了一个名为 longrun.sh 的脚本,所以实际的命令是:

                  In my case I ran a script called longrun.sh so the actual command is:

                  import subprocess
                  subprocess.Popen(["nohup", "/bin/bash", "longrun.sh"])
                  

                  我使用这个 run.py 对此进行了测试:

                  I tested this using this run.py:

                  import subprocess
                  subprocess.Popen(["nohup", "/bin/bash", "longrun.sh"])
                  print "Done!"
                  

                  我验证了(使用 ps -ax | grep longrun)longrun.sh 确实在 run.py 退出后很长时间在后台运行.

                  and I verified (using ps -ax | grep longrun) that longrun.sh does indeed run in the background long after run.py exits.

                  这篇关于如何在 bash 或 python 中在 Linux 上生成分离的后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Windows 上使用带有内置命令的 subprocess.Popen 下一篇:从 python 运行 imagemagick convert(控制台应用程序)

                  相关文章

                    <bdo id='pxElS'></bdo><ul id='pxElS'></ul>
                  1. <small id='pxElS'></small><noframes id='pxElS'>

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

                  3. <legend id='pxElS'><style id='pxElS'><dir id='pxElS'><q id='pxElS'></q></dir></style></legend>

                    1. <tfoot id='pxElS'></tfoot>