Automatically restart the Web service according to the system load value. The

  
VPS is basically the same as the general stand-alone Linux server. There is basically no difference in the use, because the virtualization technology is very mature now, but there is still a big difference between the VPS and the single service. It is the system resources and performance. Usually, VPS is divided by a separate server, so in terms of resources and performance advantages, it is far less than a single service.

Take this some time ago by VPS DDOS attack, in fact, DDOS also use a large number of requests initiated, resulting in being attacked VPS (or server) memory resources are exhausted, the system load is too high, which led to 80 The port's Http service is unresponsive, thus achieving the ultimate attack. Although DDOS can't be defended, there are always ways to reduce the impact of DDOS. For example, to periodically monitor the system status and then automatically restart the Web service, this is also a method to be introduced in this article.

when VPS attack, there are some indicators of the system can react if the system is under attack, a view that the remaining amount of memory, of course, this is not the most accurate method, another is to look The average system load value of Linux.

Linux system load average load introduction

system is defined as a process tree running average queue within a specific time interval. If a process satisfies the following conditions, it will be in the run queue:

- It is not waiting for the result of the I/O operation

- It does not actively enter the wait state (that is, there is no call) 'wait')

- is not stopped (eg: waiting termination)

Linux system, many commands can be viewed by the current system load average value, for example: w, top or uptime The command



command output indicates the average number of processes in the run queue in the past 1, 5, and 15 minutes.

Under normal circumstances, these values ​​will have some differences depending on the number of CPUs in the system. For a single-core CPU, if the load value reaches 1, it indicates that the system load has reached 100%, but for the dual-core system. In terms of 1, only the system load value is only about 50%.

and so on, in multiprocessor systems, the load mean is determined based on the number of cores. Calculated at 100% load, 1.00 represents a single processor, while 2.00 means there are two dual processors, then 4.00 means the host has four processors.

Automatically restart the web service according to the system load value

The following script uses the VPS with the Apache server as an example to periodically judge the system load value. When it reaches 5, the system is considered to be overloaded. Run, at this time, the script will automatically restart the Apache service, release system resources, in order to achieve smooth operation of the VPS server.

*Note: The script comes from the network, not original, the source is unknown, I made a few changes.


#!/bin/sh
#usage: */2 * * * * root /root/checkload.sh >>/root/checkload.log

TOP_SYS_LOAD_NUM=5
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 "AutoStart:" $(date +"%y-%m -%d %H:%M:%S") "pkill httpd" `ps -ef grep httpd wc -l`
pkill httpd
sleep 10
for i in 1 2 3
do
if [ `pgrep httpd wc -l` -le 0 ]
then
service httpd start
sleep 15
echo "AutoStart:" $(date +"%y-%m- %d %H:%M:%S") "start httpd" `ps -ef grep httpd wc -l`
fi
done
else
if [ `pgrep httpd wc -l` -le 0 ]
then
service httpd start
sleep 15
echo "AutoStart:" $(date +"%y-%m-%d %H:%M:%S") "start httpd" `ps -ef grep httpd wc -l`
fi
fi


The TOP_SYS_LOAD_NUM in the script indicates the maximum allowable system average load value, when this value is exceeded The script starts to restart the apache service. The use of the script is also very simple, directly to /etc/cron.d/to create a timed execution file, fill in the following content:


*/2 * * * * root /root/checkload .sh >>/root/checkload.log


where */2 means that the specified script is executed every two minutes and the execution result is output to the /root/checkload.log file. .

You can also modify the execution frequency according to your needs.

Summary

This script has been deployed to my VPS for a while, and from the usage situation, the effect is quite good.

Copyright © Windows knowledge All Rights Reserved