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
Use runlike to view the docker run startup parame...
Table of contents MySQL query tree structure 1. A...
Export: docker save -o centos.tar centos:latest #...
This article example shares the specific code of ...
The team replaced the new frame. All new business...
Preface The most widely used database in Linux is...
This article shares with you the specific code of...
1. Common usage: (1) Use with % % represents a wi...
Preface: js is a single-threaded language, so it ...
GitHub address: https://github.com/dmhsq/dmhsq-my...
1. ref is copied, the view will be updated If you...
The first and most important step is how to insta...
Because I wrote the word transition incorrectly i...
A process is a program code that runs in the CPU ...
Table of contents 1. Introduction 2. Understand t...