1. Install the psutil package1.1. Install the compressed package: Log in to CentOS as root and execute the following commands in sequence: wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz If wget cannot download it, copy and paste the link https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz directly in the browser to download it, use the ftp transfer tool to transfer it to Linux, and then perform the following operations 1.2 Unzip: 1.3 Enter the unzipped directory: 1.4 Start the installation: 1.5 Verification: Enter python3 in the command window, then enter: If import psutil does not report an error, the installation is successful. Then enter: res = psutil.process_iter() for var in res: print(var) A bunch of information will be displayed Second, find the name and pid of all services in the operating systemimport psutil proc_dict = {} #ID: process name proc_name = set() #About the process set, set deduplication for p in psutil.process_iter(): proc_dict[p.pid] = p.name() proc_name.add(p.name()) print(proc_dict) print('\n\n--------------------------\n') print(proc_name) 3. Find the service that needs to be monitored but is not startedproc_stop = monitor_name - proc_name That is: the name of the service to be monitored minus the names of all services in the operating system 4. Monitoring service, if it is not enabled, it will be enabled automatically, and an email will be sent to notify the staff. 1. Enable the service using os.system(command). import psutil import os #If you need to send an email to notify the administrator, you may need to use request and json #import request #import json import time time_now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) #Service to be monitored monitor_name = {'nginx'} #Start command monitor_map = {'nginx':'service nginx start'} while True: #Dictionary of all processes in the operating system proc_dict = {} #Set of all process names in the operating system proc_name = set() #psutil.process_iter() will return all process names and process IDs in the operating system for p in psutil.process_iter(): proc_dict[p.pid] = p.name() proc_name.add(p.name()) print(proc_dict) print('\n\n--------------------------\n') print(proc_name) # monitor_name - proc_name == died process name #Extract the monitoring process that has not started proc_stop = monitor_name - proc_name print(proc_stop) if proc_stop: for p in proc_stop: p_status = 'Stop' p_name = p data ={p_status,p_name,time_now} headers = {'Content-Type':'application/json;charset=utf-8'} #send_data = json.dumps(data).encode('utf-8') #Send an email to the administrator#request.post(url=url,data=send_data,headers=headers) os.system(monitor_map[p]) proc_name = set() for p2 in psutil.process_iter(): proc_name.add(p2.name()) if p in proc_name: print("Restart successful") else: print("Restart failed") time.sleep(2000) The effect after running: This is the end of this article about Linux service monitoring and operation and maintenance. For more relevant Linux service content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: The specific implementation of div not automatically wrapping and forcing not wrapping in html
Introduction <br />Not everyone has access t...
The installation information on the Internet is u...
Recently, when I was learning docker, I found tha...
Preface This article mainly introduces the releva...
Typically, we use the following SQL statement to ...
This article example shares with you the specific...
HTML5 adds a native placeholder attribute for inp...
Specific method: (Recommended tutorial: MySQL dat...
Table of contents Configuration nfs server (nfs.s...
1. Floating layout 1. Let the fixed width div flo...
Today, when I was configuring Tomcat to access th...
animation Define an animation: /*Set a keyframe t...
1. Install MySQL: Use the following three command...
The following two functions are used in the same ...
Uses of new The function of new is to create an i...