ウェブサイトの継続的なテストのためのスクリプトを改善する必要があります。

ウェブサイトの継続的なテストのためのスクリプトを改善する必要があります。

ウェブサイトの継続的なテストのためのスクリプトを改善する必要があります。

現在、次のスクリプトを使用していますが、Webサイトがまだ実行されている間に多くの電子メールが失敗します。

#!/bin/bash
while true; do
    date > wsdown.txt ;
    cp /dev/null pingop.txt ;
    ping -i 1 -c 1 -W 1 website.com > pingop.txt ;
    sleep 1 ;
    if grep -q "64 bytes" pingop.txt ; then
        :
    else
        mutt -s "Website Down!" [email protected] < wsdown.txt ;
        sleep 10 ;
    fi
done

これで、このスクリプトを何らかの方法で考慮または改善したり、他のアプローチを使用したりできます。

ベストアンサー1

;各行の終わりにある必要はありません。これはCではありません。

以下は必要ありません:

cp /dev/null pingop.txt

スクリプトの次の行は

ping -i 1 -c 1 -W 1 google.com > pingop.txt

とにかく内容を上書きしますpingop.txtping後で送信または処理する予定がない場合は、出力をファイルに保存する必要さえありません。以下を実行してください。

if ping -i 1 -c 1 -W 1 website.com >/dev/null 2>&1
then
    sleep 1
else
    mutt -s "Website Down!" [email protected] < wsdown.txt
    sleep 10

間違った肯定に関する質問に答えるのは、pingおそらくウェブサイトが正常に機能しているかどうかをテストする最良の方法ではありません。一部のWebサイトはICMP要求に応答しません。たとえば、次のようになります。

$ ping -i 1 -c 1 -W 1 httpbin.org
PING httpbin.org (3.222.220.121) 56(84) bytes of data.

--- httpbin.org ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

しかし、http://httpbin.orgすでに起きています。website.com例を使用すると、HTTP / HTTPSを介してアクセスする可能性が高くなります。この場合は、次を使用することをお勧めしますcurl -Is

$ curl -Is "httpbin.org" >/dev/null  2>&1
$ echo $?
0
$ curl -Is "non-existing-domain-lalalala.com" >/dev/null  2>&1
$ echo $?
6

OPはコメントでpingcurl次回のウェブサイトをテストする場合には大きな違いはありませんping

$ time curl -Is google.com >/dev/null  2>&1
real    0m0.068s
user    0m0.002s
sys     0m0.001s
$ time ping -i 1 -c 1 -W 1 google.com
PING google.com (216.58.215.110) 56(84) bytes of data.
64 bytes from waw02s17-in-f14.1e100.net (216.58.215.110): icmp_seq=1 ttl=54 time=8.06 ms

--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 8.068/8.068/8.068/0.000 ms
real    0m0.061s
user    0m0.000s
sys     0m0.000s

ただし、応答しないウェブサイトをテストするとき、pingthenは現在使用されているpingcurl よりも信頼性が高いだけでなく高速です。-W

$ time ping -i 1 -c 1 -W 1 httpbin.org
PING httpbin.org (3.222.220.121) 56(84) bytes of data.

--- httpbin.org ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms


real    0m1.020s
user    0m0.000s
sys     0m0.000s
$ time curl -Is httpbin.org  >/dev/null  2>&1

real    0m0.256s
user    0m0.003s
sys     0m0.000s

おすすめ記事