Linux system real-time monitor Apache running status and automatically restart HTTPD service

Source: Internet
Author: User
Tags curl sleep ssh

In order to achieve a highly available based on the Apache web site environment, in Apache for a variety of reasons automatically stop running, want to immediately restore site access, this requires a tool to monitor the operation of Apache real-time and can automatically restart the HTTPD service, on the Internet to find relevant information, Rewrite the following simple monitoring and restart script:

Principle: Access to its own Apache service via the server (similar to the user's access to the Web), such as more than 15s does not return normal 220-head code information, indicating that the Apache service has stopped running, then immediately restart the httpd service.

Script Usage One:

1, on the Linux server to perform VI edit a new script, and the following script code to copy in, and then exit and save (need to understand some simple Linux commands, such as do not understand the message to help webmaster)

The code is as follows Copy Code

[Root@localhost/]# VI/OPT/AUTORSHTTPD
#!/bin/bash
Url= "HTTP://127.0.0.1/"
Curlit ()
{
Curl--connect-timeout--max-time--head--silent "$URL" | grep ' 200 '
}
Doit ()
{
if! Curlit; Then
/ETC/INIT.D/HTTPD Restart >/dev/null
Fi
}
While true; Todo
doit >/dev/null
Sleep 10
Done

2, give the script executable permissions

The code is as follows Copy Code

[Root@localhost/]# chmod 755/opt/autorshttpd

3. Execute script

The code is as follows Copy Code

[Root@localhost/]# SH/OPT/AUTORSHTTPD &
[Root@localhost/]# exit

Note: Why should I add a & symbol behind the sh command? This is because our general operation server is remote SSH operation, so if you do not add & symbol, then close the SSH remote interface, this process will end, plus the & symbol, even if the SSH remote can be turned off to run the program in the background, do not forget to use the Exit command to exit after landing, Close SSH remote interface again OH

4, let the script boot automatically run

The code is as follows Copy Code

[Root@localhost/]# vi/etc/rc.local

Add sh/opt/autorshttpd to the back of the line.

Method Two:

Save the following program

The code is as follows Copy Code

#!/bin/bash
Url= "HTTP://127.0.0.1/"
Curlit ()
{
Curl--connect-timeout--max-time--head--silent "$URL" | grep ' 200 '
}
Doit ()
{
if! Curlit; Then
/ETC/INIT.D/HTTPD Restart >/dev/null
Fi
}
While true; Todo
doit >/dev/null
Sleep 10
Done


, upload it to the/opt directory of the Linux server, and then execute the 2-4 steps in the method one in turn.

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.