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

How to monitor the running status of http service regularly by shell script

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you how shell scripts can monitor the running status of http services regularly. The content is simple and easy to understand and clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article. How shell scripts can monitor the running status of http services regularly?

Note: Monitoring methods can simulate access for ports, processes, URLs, or a combination of all three.

Note: Since only if statements have been mentioned so far, please use if statements to achieve this.

[root@oldboy-B scripts]# cat apachemon #!/ bin/sh #created by oldboy 20110523 . /etc/init.d/functions HTTPPRONUM=`ps -ef| grep http| grep -v grep| wc -l` #if [ $HTTPPRONUM -lt 1 ];then if [[ $HTTPPRONUM -lt 1 ]]; then action "httpd is not running" /bin/false action "httpd is not running" /bin/false >/tmp/httpd.log httpdctl restart >/dev/null 2>&1 action "httpd is restart" /bin/true mail -s "`uname -n`'s httpd restarted at `(date)`" 31333741@ qq.com exit 1 else action "httpd is running" /bin/true exit 0 fi [root@oldboy-B scripts]# apachemon httpd is running [determine] [root@oldboy-B scripts]# pkill httpd [root@ oldboy-B scripts]# ps -ef| grep http |grep -v grep [root@oldboy-B scripts]# apachemon httpd is not running [losing] httpd is restart [determining] [root@oldboy-B scripts]# ps -ef| grep http| grep -v grep root 5845 1 1 15:59 ? 00:00:00 /usr/sbin/httpd -k restart apache 5852 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart apache 5853 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart apache 5854 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart apache 5855 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart apache 5856 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart apache 5857 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart apache 5858 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart apache 5859 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

Script improvements

[root@oldboy-B /]# echo oldboytest >/var/www/html/index.htm [root@oldboy-B /]# wget -quiet -spider http://10.0.0.161/index.htm [root@oldboy-B /]# echo $? 0 [root@oldboy-B /]# ll index.htm ls: index.htm: no such file or directory [root@oldboy-B scripts]# cat apachemon1 #!/ bin/sh #created by oldboy 20110523 . /etc/init.d/functions #HTTPPRONUM=`ps -ef| grep http| grep -v grep| wc -l` #===> This is based on http mode to determine wget -quiet -spider http://10.0.0.161/index.htm #===> This is based on WGET URL mode to determine if [ $? -ne 0 ];then action "httpd is not running" /bin/false >/tmp/httpd.log httpdctl restart >/dev/null 2>&1 action "httpd is restart" /bin/true >>/tmp/httpd.log mail -s "`uname -n`'s httpd restarted at `(date)`" mail@qq.com exit 1 else action "httpd is running" /bin/true exit 0 fi

In real use, some output is not needed to remove

[root@oldboy-B scripts]# cat apachemon1 #!/ bin/sh #created by oldboy 20110523 # . /etc/init.d/functions wget -quiet -spider http://10.0.0.161/index.htm #===> This is based on the WGET URL method to determine if [ $? -ne 0 ];then action "httpd is not running" /bin/false >/tmp/httpd.log httpdctl restart >/dev/null 2>&1 action "httpd is restart" /bin/true >>/tmp/httpd.log mail -s "`uname -n`'s httpd restarted at `(date)`" 31333741@qq.com exit 1 fi

The Writing of Multiple Conditional Judgments

[root@oldboy-B scripts]# cat apachemon1 #!/ bin/sh #created by oldboy 20110523 # . /etc/init.d/functions HTTPPORTNUM=`netstat -lnt|grep 80|grep -v grep|wc -l` HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l` wget -quiet -spider http://10.0.0.161/index.htm && RETVAL=$? if [ $RETVAL -ne 0 ] || [ $HTTPPORTNUM -ne 1 ] || [ $HTTPPRONUM -lt 1 ] ;then #if [ "$RETVAL" != "0" -o "$HTTPPORTNUM" != "1" -o "$HTTPPRONUM" \

< "1" ] ;then  action "httpd is not running" /bin/false  action "httpd is not running" /bin/false >

/tmp/httpd.log httpdctl restart >/dev/null 2>&1 action "httpd is restart" /bin/true mail -s "`uname -n`'s httpd restarted at `(date)`" 3133741@qq.com exit 1 else action "httpd is running" /bin/true exit 0 fi above is "shell script how to implement regular monitoring http service running status" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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

Servers

Wechat

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

12
Report