如何使用Linux检查网站是否向上/向下检查

发布于 2025-02-08 22:47:30 字数 414 浏览 1 评论 0原文

#!/usr/bin/bash
MYHOST="https://www.testwebsite.com"
#Check if Website is up or down
if wget -S $MYHOST 2>&1 | grep -w "200\|301\" > /dev/null
then
echo "Website is up"
else
#Website is down"
Send mail / mailx to send email

脚本可以发送电子邮件警报,如果网站关闭,但它会发送重复的电子邮件直到启动。

如何:

  1. 如果网站关闭,请发送电子邮件警报,但应仅发送一次警报。
  2. 如果已经启动,则应将电子邮件发送为UP。

网站上及以上的一封电子邮件。

#!/usr/bin/bash
MYHOST="https://www.testwebsite.com"
#Check if Website is up or down
if wget -S $MYHOST 2>&1 | grep -w "200\|301\" > /dev/null
then
echo "Website is up"
else
#Website is down"
Send mail / mailx to send email

Script can send an email alert if website is down but it sends repeated emails until it is up.

How to:

  1. Send an email alert if website is down but alert should be sent only once.
  2. If it is up, email should be sent as up.

One email for website down and up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み零 2025-02-15 22:47:30

我认为,我将在一段时间内包装整个IF语句,并创建一个变量以指示是否发送了邮件

#!/usr/bin/bash
MYHOST="https://www.testwebsite.com" # website
sent=false # sent?

while true
do
  get=$(wget -S $MYHOST 2>&1 | grep -w "200\|301")
  if [ "$get" != "" ] # wget to the website
  then
    # website is up
    echo "Do Something"
  else
    # website is down
    if ! $sent # if the email hasn't been sent yet
    then
      mailx ...
      sent=true
    fi
  fi
done

In my opinion, I would wrap the whole if statement in a while and create a variable to indicate if the mail is sent

#!/usr/bin/bash
MYHOST="https://www.testwebsite.com" # website
sent=false # sent?

while true
do
  get=$(wget -S $MYHOST 2>&1 | grep -w "200\|301")
  if [ "$get" != "" ] # wget to the website
  then
    # website is up
    echo "Do Something"
  else
    # website is down
    if ! $sent # if the email hasn't been sent yet
    then
      mailx ...
      sent=true
    fi
  fi
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文