Multi-server monitor / auto repair in python

I have 2 server programs that need to be run using the GNU screen. I would like to harden these servers from crashes using a Python-based program that starts every screen session and then controls the server process. If the server process crashes, I need python code to kill an extraneous screen session and restart the server using the screen again.

I am very new to python, but I use this opportunity to teach myself. I know this can be done in bash scripts. But I want to use this code for future functions, so it should 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 } 
+3
source share
2 answers

"must be multithreaded to handle the restart of two separate programs"

Do not understand 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) 
+5
source

You really do not have to run the software on screen. If the server will reboot, how do you start it? Manually? I also think that you are trying to invent a wheel. There are already pretty good tools that do what you need.

launchtool allows you to launch a user command that controls it in many ways, such as managing its environment, blocking signals, recording its output, changing user and group permissions, restricting resource use, reloading it if it does not work, constantly working, turning it into a daemon etc.

.

Monit is a free open source utility for managing and monitoring processes, files, directories and file systems on a UNIX system. the monitor performs automatic maintenance and repair and can perform significant causal actions in error situations.

+3
source

Source: https://habr.com/ru/post/1337553/


All Articles