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 does Shell monitor the operation of the website URL?

2025-02-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you the relevant knowledge about how Shell monitors the operation of the website URL. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Wget command

The wget command has a lot of parameters, at least dozens of them, but not many are commonly used in operation and maintenance. The old boy teacher listed the most useful ones as follows.

Wget commands download pages or files-spider simulates the crawler's behavior to visit the site, but does not download the page-Q,-- quiet quiet access, forbids output, similar to-o / dev/null function-o,-- output-file=FILE records output to file-T,-- timeout=SECONDS timeout to visit the site-t,-- the number of times tries=NUMBER retries the site when the site is abnormal

The actual monitoring method is as follows: use the return value of wget command to determine whether the website is normal or not

[root@oldboy ~] # wget-- spider-T 5-Q-t 2 www.oldboyedu.com [root@oldboy ~] # echo $? # 0curl command

The parameters of the curl command are more than those of wget, but there are not many parameters commonly used in operation and maintenance, so the list is as follows:

Curl command access website url-I/--head displays response header information-time of m/--max-time access timeout-o/--output records access information to file-s/--silent silent mode access, that is, does not output information-w/--write-out is output in a fixed special format, for example:% {http_code}, output status code actual monitoring method

1. Use the return value of curl command to determine whether the website is normal or not.

[root@oldboy ~] # curl-s-o / dev/null www.oldboyedu.com [root@oldboy ~] # echo $? 0

2. Get the status code after the command is executed (200 indicates normal)

[root@oldboy] # curl-I-m 5-s-w "% {http_code}\ n"-o / dev/null www.baidu.com 200

3 develop Shell script to monitor whether the specified URL is normal or not

Answer: method 1: #! / bin/sh function usage () {# echo $"usage:$0 url" exit 1} function check_url () {# wget-- spider-Q-o / dev/null-- tries=1-T 5 $1 # # curl-s-o / dev/null $1 # if [$?-eq 0] then echo "$1 is yes." Exit 0 else echo "$1 is fail." Exit 1 fi} function main () {# if [$#-ne 1] # then usage fi check_url $1 #} main $* # practice result [root@oldboy] # sh checkurl.sh www.oldboyedu.com www.oldboyedu.com is ok [root@oldboy] # sh checkurl.sh www.baidu.com www.baidu.com is ok [root@oldboy] # sh checkurl.sh 10.0.0.8 10.0.0. 8 is fail method 2: [root@oldboy ~] # cat checkurl1.sh #! / bin/bash # File Name: checkurl.sh # Version: V1.0 # Author: oldboy # Organization: www.oldboyedu.com # Created Time: 2018-06-07 18:29:19 # usage () {echo "Usage:$0 url" exit 1} checkurl () {local num= `curl-I-m 5-s-w "% {http_code}\ N "- o / dev/null | grep 200 | wc-l` if [$num-eq 1] # then echo" $1 is ok "else echo" $1 is fail "fi} main () {if [$#-ne 1] then usage fi checkurl $1} main $* practice result [root@oldboy ~] # sh checkurl.sh www.oldboyedu.com www.oldboyedu.com is ok [root@oldboy ~] # sh checkurl.sh www.baidu.com www.baidu.com is ok [root@oldboy ~] # sh checkurl.sh 10.0.0.8 10.0.0.8 is fail

If netizens can read this article, I believe it will be easy to write a script to monitor the website URL in the future.

These are all the contents of the article "how Shell monitors the operation of the website URL". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please 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

Development

Wechat

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

12
Report