多服务器监控/ Python中自动重新启动器 [英] Multi-server monitor/auto restarter in python

查看:147
本文介绍了多服务器监控/ Python中自动重新启动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必须使用GNU屏幕的启动2服务器程序。我想用基于Python的节目揭开序幕每个屏幕会话然后监控服务器过程强化对崩溃这些服务器。如果服务器进程崩溃,我需要蟒蛇code杀死外来屏幕会话并再次重新启动与屏幕上的服务器。

我很新的蟒蛇,但我用这个机会来教我。我知道这可以在bash脚本编程来完成。但我想在此建code为未来的功能,因此它需要只蟒蛇。

伪code是如下:

 线程一{
     虽然1:
     使用屏幕启动服务器1
     等待服务器结束
     而结束
}螺纹二{
     虽然1:
     使用屏幕启动服务器2
     等待服务器结束
     而结束
}


解决方案

需要多线程处理的两个单独的程序重新开始

不知道为什么。

 导入子命令= [[P1],[P2]]
程序= subprocess.Popen(三)对于C的命令]
而真正的:
    因为我在范围内(LEN(程序)):
        如果程序[I] .return code是无:
            继续#仍在运行
        其他:
            #重启这一
            节目由[i] = subprocess.Popen(命令[I])
        time.sleep(1.0)

I have 2 server programs that must be started with the use of GNU Screen. I'd like to harden these servers against crashes with a Python based program that kicks off each screen session then monitors the server process. If the server process crashes, I need the python code to kill the extraneous screen session and restart the server with screen again.

I'm very new to python but I'm using this opportunity to teach myself. I'm aware this can be done in bash scripting. But I want to build on this code for future features, so it needs to be just python.

The pseudocode is as follows:

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
}

解决方案

"need to be multi-threaded to handle the restarting of two separate programs"

Don't see why.

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中自动重新启动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆