Requirements: The software services provided by software companies will inexplicably hang up on the server. The only way they use is to restart the server. The restart time is too long, so they want to create a program to monitor processes and services. If they detect that they are suspended, they will restart the process or service.
import psutil import time import subprocess import logging import import wmi import ctypes import os # Get service statusdef get_service_status(service_names): wmiobj = () for service_name in service_names: services = wmiobj.Win32_Service(Name = service_name) if services: # If the service status is stopped, start the service if services[0].state == "Stopped": start_service(service_name) # return services[0].state else: (f"fail:Serve{service_name}Does not exist") #Determine whether there is administrator privilegesdef is_admin(): try: return .() except: return False # Start the servicedef start_service(service_name): if is_admin(): try: cmd = 'NET START {}'.format(service_name) (cmd) (f"success:启动了Serve{service_name}") except Exception as e: (f"fail:启动Serve{service_name}fail, error message: {e}") else: (f"Not admin permission") # Log configurationdef loggingstars(): logger = () () handler = () (handler) file_handler = (filename='process_name.log', maxBytes=1024*1024*90, backupCount=10) file_handler.setLevel() formatter = ('%(asctime)s - %(levelname)s - %(message)s') file_handler.setFormatter(formatter) (file_handler) # Get the complete execution path of the disconnected processdef find_process_by_name(process_names): all__process = set() """Get the completion path of all processes""" for proc in psutil.process_iter(['pid', 'name','exe']): all__process.add(['exe']) """Find out the complete path to the dropped monitoring process""" new_list = [item for item in process_names if item not in all__process] return new_list def start_process(process_names, service_anmes): while True: # Check whether the service is started get_service_status(service_anmes) (60) stop_process = find_process_by_name(process_names) if not stop_process: continue """Start the disconnected process""" for app in stop_process: try: (f"{app}") (f"success:The process started{app}") except Exception as e: (f"fail:Start the process{app}fail, error message: {e}") # If you need to start the process, pause for 20 minutes (20*60) if __name__ == "__main__": loggingstars() ("============== Process monitoring program starts ============") # The process that needs to be monitored, why use the executable file? It is because it is found that the process name will be the same process_names = [ "C:\\UserApp\\app\\baidu-translate-client\\Baidu Translation.exe", "C:\\UserApp\\app\\dingding\\main\\current\\" ] # Name of service that needs to be monitored service_anmes = ["MySQL80", "JetBrainsEtwHost"] start_process(process_names, service_anmes)
In fact, the service also starts the execution file and then has a process. If you find all the processes of the services, just write process monitoring directly. Here I just show how to monitor the services and processes.
Some services or processes may be started in sequence
- Open Run Enter "msinfo32"
- Select "Software Environment" and then click "Running Tasks" to view the startup time suspiciously
Use Pyinstaller -F to package it and put it on the server
This is the article about python monitoring the process and services of windows servers. For more related python monitoring the process and service content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!