Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use the Shell script to monitor whether the website URL is normal

Shulou Source: shulou.com Published: 2022-06-01 03:09:22 09月22日 Update

This article introduces you how to use Shell script program to monitor whether the website URL is normal, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The most common way to monitor whether the website URL is normal is the wget and curl commands, both of which are so powerful that netizens can't choose to check the help. today, the old boy teacher happens to record a practical course on Shell programming.

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 command downloads web pages or files-spider simulates the crawler's behavior to visit the website, but does not download web pages-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,-- 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 $? # < = use the returned value to determine whether the website is normal. 0

Curl 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-m/--max-time access timeout-o/--output records access information to a 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 () {# < = = help function echo $"usage:$0 url" exit 1} function check_url () {#

Tags: Commands websites functions parameters methods monitoring help output scripts information that is commonly used files status programs powerful content actual time more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn macOS NVidia Apple