Operation and Maintenance Enterprise Interview Question 1 Monitor whether MySQL master-slave synchronization is abnormal linux crontab & execute it every 10 seconds

 

Practical programming written test questions for Linux operation and maintenance (19 questions)

 

Enterprise interview question 1 : (Production case): Monitor whether MySQL master-slave synchronization is abnormal. If it is abnormal, send SMS or email to the administrator. Tip: If there is no master-slave synchronization environment, you can use the following text to read into a file to simulate:

Phase 1: Develop a daemon script to implement detection every 30 seconds.

Phase 2: If the synchronization occurs with the following error numbers (1158, 1159, 1008, 1007, 1062), the error is skipped.

Stage 3: Please use array technology to implement the above script (obtain the master-slave judgment and error number part)

#Assume the file is /log/test/mysql/err
#Assume the error is: error:1120:...
#!/bin/bash
#
#Monitor whether the MySQL master-slave synchronization is abnormal. If it is abnormal, send a text message or email to the administrator. Tip: If there is no master-slave synchronization environment, you can use the following text to read into a file to simulate:
# Phase 1: Develop a daemon script to implement detection every 30 seconds.
#Phase 2: If the synchronization occurs with the following error numbers ( 1158 , 1159 , 1008 , 1007 , 1062 ), skip errors.
#Stage 3: Please use array technology to implement the above script (obtain the master-slave judgment and error number part)
#Assume the file is /log/test/mysql/ err
#Assume the error is: error: 1120 :...
#
#version 0.1
#

errlocate='/log/test/mysql/err'
declare -a errnums
declare -i j=0

function msgbox(){
    echo "MySQL error number is $1" | mail -s mysql.error.alert root
}

while true; do
    if [ `grep 'error:[[:digit:]]\+:' ${errlocate}` ];then
        for i in `grep -o 'error:[[:digit:]]\+:' ${errlocate} | grep -o '[[:digit:]]\+'` ; do
            errnums[$j]=$i;
            case ${errnums[$j]} in
                1158) continue;;
                1159) continue;;
                1008) continue;;
                1007 ) continue;;
                 1062 ) continue;;
                 * );;
             esac 
            # echo  " mysql.errnums=${errnums[$j]} " for testing
            msgbox ${errnums[$j]}
            let j++
            # echo  " j=$j " test 
        done 
        # echo  " it should be sleeped " test 
        sleep 30s
        j=0
    else
        sleep 30s
        #Wait for 30s without error
    fi     
done

Running it in the background:

-113-[root@vm]18:25 /tmp/sh # chmod -x ywtest1.sh
-114-[root@vm]18:26 /tmp/sh # .ywtest1.sh &

However, this method is not absolutely reliable. When the pstree command is used again, it will automatically exit:

Use crontab -e to create a daemon process, and also cancel the while loop in the source code

refer to:

linux crontab & execute every 10 seconds

-115-[root@vm]18:25 /tmp/sh # crontab -e
* * * * * /bin/bash /tmp/sh/ywtest1.sh
* * * * * sleep 30s;/bin/bash /tmp/sh/ywtest1.sh

 

Take a look at the execution effect:

 

Two emails per minute, i.e. one in 30s

 

Please look forward to the follow-up

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325219833&siteId=291194637