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.