Simple implementation method of Linux process monitoring and automatic restart

Simple implementation method of Linux process monitoring and automatic restart

Purpose:

Under Linux, the server program may be dumped for various reasons, which will affect user use. Here we provide a simple process monitoring and restart function.

Implementation principle:

The script is called by the scheduled task crontab. The script uses ps to check whether the process exists. If it does not exist, it restarts and writes a log.

Crontab modification

# crontab -e
*/5 * * * * /mnt/bindmonitor.sh

Implementation of /mnt/bindmonitor.sh

#!/bin/sh

host_dir=`echo ~` # Current user root directory proc_name="/home/wkubuntu/named/sbin/named" # Process name file_name="/mnt/bindmonitor.log" # Log file pid=0

proc_num() # Calculate the number of processes {
 num=`ps -ef | grep $proc_name | grep -v grep | wc -l`
 return $num
}

proc_id() # process number {
 pid=`ps -ef | grep $proc_name | grep -v grep | awk '{print $2}'`
}

proc_num
number=$?
if [ $number -eq 0 ] # Check if the process exists then 
 /home/wkubuntu/named/sbin/named -c /home/wkubuntu/named/etc/named.conf -n 1 &
              # Command to restart the process, please modify proc_id accordingly # Get the new process ID echo ${pid}, `date` >> $file_name # Record the new process ID and restart time to file

Deleting process tests

a. #killall -15 named

b. After 5 minutes, run cat /mnt/bindmonitor.log to see if there are any new records. The process number should correspond to the process number in # ps -ef |grep named.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the implementation method of Linux monitoring important processes
  • Shell script to implement Linux system and process resource monitoring
  • Linux service monitoring and operation and maintenance
  • Linux operation and maintenance basic process management real-time monitoring and control

<<:  Analysis of pitfalls in rounding operation of ROUND function in MySQL

>>:  Vue Element front-end application development conventional Element interface components

Recommend

How to insert 10 million records into a MySQL database table in 88 seconds

The database I use is MySQL database version 5.7 ...

CentOS7.x uninstall and install MySQL5.7 operation process and encoding format modification method

1. Uninstalling MySQL 5.7 1.1查看yum是否安裝過mysql cd y...

A simple example of creating a thin line table in html

Regarding how to create this thin-line table, a s...

Implementation and optimization of MySql subquery IN

Table of contents Why is IN slow? Which is faster...

Detailed tutorial on configuring nginx for https encrypted access

environment: 1 CentOS Linux release 7.5.1804 (Cor...

How to reference external CSS files and iconfont in WeChat applet wxss

cause The way to import external files into a min...

How to build a multi-node Elastic stack cluster on RHEL8 /CentOS8

Elastic stack, commonly known as ELK stack, is a ...

TypeScript decorator definition

Table of contents 1. Concept 1.1 Definition 1.2 D...

Docker image export, import and copy example analysis

The first solution is to push the image to a publ...

Detailed steps for deploying Tomcat server based on IDEA

Table of contents Introduction Step 1 Step 2: Cre...

In-depth understanding of asynchronous waiting in Javascript

In this article, we’ll explore how async/await is...

HTML+CSS to achieve simple navigation bar function

Without further ado, I'll go straight to the ...

11 Reasons Why Bootstrap Is So Popular

Preface Bootstrap, the most popular front-end dev...