How Linux implements process monitoring and guarding

Source: Internet
Author: User

Recently, the newly built Amazon EC2 Server, which deploys a static web, initiates an nginx proxy. Recently found a problem:

Nginx process interval time on the inexplicable hang off, and then there is the site can not open the dilemma.

To prevent this problem from happening again, use the following method to do the Nginx daemon.

1. Script, monitor the nginx process, if it hangs, restart, otherwise do not intervene.

Under the/data/work/scripts directory, create a restart_nginx.sh file that reads as follows:

#查找nginx进程, exclude the process number generated by the grep command, and awk prints the 2nd column value (that is, the process number) assigned to the PID variable pid=`PSAux |grepNginx |grep-Vgrep|awk '{print $}'`
#记录当前时间dat=`Date '+%y-%m-%d%h:%m:%s'' #输出当前时间Echo$dat
#输出进程号Echo$pid # True if the length of the string is greater than 0 (the process number is not empty)if[-N"$pid" ] Then{
# show that the process is still alive, no processingEcho===========alive!================}Else
#否则进程挂了, reboot requiredEcho===========shutdown!start==============/opt/tengine/sbin/Nginxfi

2. Grant restart_nginx.sh File executable permissions

chmod u+x Restart_nginx. SH

3. Edit the Linux timer, add timed tasks, execute restart_nginx.sh script every 2 minutes

Crontab-e

Paste the following content:

*/2 * * * * */data/work/scripts/restart_nginx. sh >>/data/work/scripts/restart_nginx.log

4. Restart the timer

/etc/init.d/crond restart

5. Check the status of the current Nginx process first

Found four processes related to Nginx.

6. Check the timer log:

Indicates that the process is normal at this time.

6. In order to test the Nginx hangs can automatically restart, we manually kill Nginx

PS grep ' Nginx ' grep grep awk ' {print $} ' Xargs Kill -9

At this time Nginx has been killed, we wait two minutes to see if the process can be automatically restored under the timer.

At this point, the current process has started!

How Linux implements process monitoring and guarding

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.