python - python中的多服务器监视器/自动重启器

标签 python linux bash restart

我有 2 个必须使用 GNU Screen 启动的服务器程序。我想使用基于 Python 的程序来加强这些服务器以防止崩溃,该程序启动每个屏幕 session 然后监视服务器进程。如果服务器进程崩溃,我需要 python 代码来终止无关的屏幕 session 并重新启动带有屏幕的服务器。

我是 Python 的新手,但我正在利用这个机会自学。我知道这可以在 bash 脚本中完成。但我想在这段代码的基础上构建 future 的功能,所以它需要只是 python。

伪代码如下:

thread-one {
     While 1:
     start server 1 using screen
     wait for server to end
     end while
}

thread-two {
     While 1:
     start server 2 using screen
     wait for server to end
     end while
}

最佳答案

“需要多线程来处理两个独立程序的重启”

不明白为什么。

import subprocess

commands = [ ["p1"], ["p2"] ]
programs = [ subprocess.Popen(c) for c in commands ]
while True:
    for i in range(len(programs)):
        if programs[i].returncode is None:
            continue # still running
        else:
            # restart this one
            programs[i]= subprocess.Popen(commands[i])
        time.sleep(1.0)

关于python - python中的多服务器监视器/自动重启器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/894474/

相关文章:

python - "New Console for notebook"用于 Visual Studio 代码中的 jupyter 笔记本

python - 如何在 Python 中重载?

python - 如何根据单个scrapy.Spider的不同命令设置不同的IP?

linux - shell脚本中的成绩计算

linux - zsh 中修改当前完成选项的 compopt 等效项是什么?

linux - 将最新修改的文​​件从所有子目录复制到具有相同子目录结构的新目录

python - 循环中的 Tkinter 名称小部件

linux - 如何表达 "map a linux command to each line in a file"?

bash - 在 Bash/Shell 中使用 "Paste"和 "- - -"

linux - 脚本杀死太长的进程