Note:
Operating system: CentOS
Web server: apache
Problem:
Search engine crawlers increase website access volume in a certain period of time. The server is under heavy pressure, the website access times out, and the page cannot be opened.
Solution:
Regularly monitors server load. After a threshold value is exceeded, the web service is automatically restarted.
Apache installation directory:/usr/local/apache
Script running log file:/usr/local/apache/check_cpu_load.log
Script storage directory:/home/crontab
Cd/home/crontab
Vi check_cpu_load.sh # edit and add the following code
#! /Bin/sh
# Crontab: */5 *****/home/crontab/check_cpu_load.sh>/usr/local/apache/check_cpu_load.log
TOP_SYS_LOAD_NUM = 3
SYS_LOAD_NUM = 'uptime | awk' {print $ (NF-2)} '| sed's /,//''
Echo $ (date + "% y-% m-% d") 'uptime'
If ['echo "$ TOP_SYS_LOAD_NUM <$ SYS_LOAD_NUM" | bc '-eq 1]
Then
Echo "#0 #" $ (date + "% y-% m-% d % H: % M: % S ") "pkill httpd" 'PS-ef | grep httpd | wc-L'>/usr/local/apache/check_cpu_load.log
/Usr/local/apache/bin/apachectl stop
Sleep 5
Pkill httpd
Sleep 5
For I in 1 2 3
Do
If ['pgrep httpd | wc-L'-le 0]
Then
/Usr/local/apache/bin/apachectl start
Sleep 30
Echo "#1 #" $ (date + "% y-% m-% d % H: % M: % S ") "start httpd" 'PS-ef | grep httpd | wc-L'>/usr/local/apache/check_cpu_load.log
Fi
Done
Else
If ['pgrep httpd | wc-L'-le 0]
Then
/Usr/local/apache/bin/apachectl start
Sleep 30
Echo "#2 #" $ (date + "% y-% m-% d % H: % M: % S ") "start httpd" 'PS-ef | grep httpd | wc-L'>/usr/local/apache/check_cpu_load.log
Fi
Fi
: Wq! # Save and exit
Crontab-e # Add a scheduled task
*/5 */home/crontab/check_cpu_load.sh>/usr/local/apache/check_cpu_load.log
: Wq! # Save and exit
Service crond restart # restart a scheduled task
After the preceding settings, when the server load exceeds 3, the script is automatically run within 5 minutes, and the apache service is restarted to restore the website access.