Get the App
SLTechnology News&Howtos  ›  Servers  › 

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

Shulou Source: shulou.com Published: 2022-06-01 03:00:29 10月15日 Update

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!

Tags: Script monitoring mode status service run content article method statement learning help writing home file easy to understand more condition organization directory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Redmi Shulou Information MySQL