In CentOS, the Web server's CPU load exceeds the threshold and the shell script is automatically restarted.

Source: Internet
Author: User
Tags centos pkill

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.