Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

For example, the Shell script that implements intelligent monitoring to automatically restart the Apache server for deterioration.

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "Shell script for automatic restart of Apache server by intelligent monitoring for example deterioration". In daily operation, it is believed that many people have doubts about the problem of Shell script such as deterioration for automatic restart of Apache server with intelligent monitoring. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubts of "such as worsening the Shell script that automatically restarts the Apache server for intelligent monitoring"! Next, please follow the editor to study!

Functions and characteristics of script

1. Be able to monitor the availability of the Apache server at regular intervals (since this script directly simulates the access of the client, the "availability" here refers to real normal access)

2. In the event of inaccessibility, the Apache service can be automatically restarted (forced restart)

3. If there is still no normal access after restart, further operations (such as sending email, restarting the server, etc.) will be performed automatically.

Later, we will give the specific usage and comments of this script, which can be manually modified according to your own situation (you need to know some bash shell programming).

Content of the script:

#! / bin/bashURL= "http://127.0.0.1/"curlit(){curl-connect-timeout 15-max-time 20-head-silent" $URL "| 15 above grep '200 calls # is the connection timeout. If the HTTP service of localhost is accessed for more than 15 seconds and still fails to respond correctly to 200 codes, it is determined that it cannot be accessed. } doit () {if! Curlit; then# if the apache service of localhost does not return 200 headers normally, an exception occurs. Execute the following command: sleep 20top-n 1-b > > / var/log/apachemonitor.log# write the contents of the top command to the file for reference / usr/bin/killall-9 apache2 & & / usr/bin/killall-9 php5-cgi & & / usr/bin/killall-9 httpd & & / usr/bin/killall-9 http & & / usr/bin/killall-9 apache & & / usr/bin/killall-9 php-cgi > / dev/null# compatibility Killed all kinds of apache processes. You can modify sleep 2/etc/init.d/apache2 start > / dev/null/etc/init.d/httpd start > / dev/null# according to the characteristics of your apache service. For the sake of compatibility, you have executed two apache restart commands, which can be modified as needed. Echo $(date) "Apache Restart" > > / var/log/apachemonitor.log# write log sleep 3 write log wait 30 seconds after the restart is complete, and then try if again! If curlit; then# is still not accessible, then: echo $(date) "Failed! Now Reboot Computer! "> > / var/log/apachemonitor.log# writes apache to the log that still fails and reboot# restarts the machine. In fact, restarting the entire server is a very last resort. I do not recommend it. Everyone modifies it according to their own needs, such as text messages, e-mail and so on. Fisleep 180fi} sleep 30 runs the script 5 minutes later to start formal work (to prevent misjudgment due to the fact that apache hasn't started after restarting the server) while true; do# main loop body doit > / dev/nullsleep 10done

Usage

The method of use is very flexible, in short, just let the above script be executed all the time after boot. Because the script uses the curl command, you also need to install the curl environment. How to install curl:

Select the following two commands according to the distribution:

Yum install curlapt-get install curl

If you want to start it automatically, you can write the script to rc.local.

At this point, the study on the "Shell script for intelligent monitoring to automatically restart the Apache server" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report