Simple implementation of linux process monitoring and automatic restart
- 2021-06-28 14:49:43
- OfStack
Purpose:
The server program under linux will drop dump for various reasons, which will affect user use. Here is a simple process monitoring and restart function.
Implementation principle:
The script is invoked by the timer task crontab, which checks for the existence of the process using ps, and restarts and writes to the log if it does not exist.
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 ] # Determine whether a process exists
then
/home/wkubuntu/named/sbin/named -c /home/wkubuntu/named/etc/named.conf -n 1 &
# The command to restart the process, please modify it accordingly
proc_id # Get the new process number
echo ${pid}, `date` >> $file_name # Record new process number and restart time
fi
Delete Process Tests
a. #killall -15 named
After b. 5 minutes, cat/mnt/bindmonitor.log checks to see if there is an up-to-date record. The process number corresponds to the # ps-ef | grep named process number.
summary