Linux Bash-Ping站点检查脚本。(更长的超时时间)

Linux Bash-Ping站点检查脚本。(更长的超时时间),linux,bash,ping,Linux,Bash,Ping,我目前正在使用Raspberry PI作为ping服务器,这是我用来检查ok响应的脚本 我不熟悉bash脚本,所以对于curl调用来说,这是一个初学者的问题,有没有办法增加超时时间,因为它会不断报告错误 #!/bin/bash SITESFILE=/sites.txt #list the sites you want to monitor in this file EMAILS=" " #list of email addresses to receive alerts (co

我目前正在使用Raspberry PI作为ping服务器,这是我用来检查ok响应的脚本

我不熟悉bash脚本,所以对于curl调用来说,这是一个初学者的问题,有没有办法增加超时时间,因为它会不断报告错误

#!/bin/bash

SITESFILE=/sites.txt #list the sites you want to monitor in this file
EMAILS="         " #list of email addresses to receive alerts (comma separated) 

while read site; do
  if [ ! -z "${site}" ]; then 

    CURL=$(curl -s --head $site)

    if echo $CURL | grep "200 OK" > /dev/null
    then
        echo "The HTTP server on ${site} is up!"
        sleep 2
    else

        MESSAGE="This is an alert that your site ${site} has failed to respond 200 OK."

        for EMAIL in $(echo $EMAILS | tr "," " "); do
            SUBJECT="$site (http) Failed"
            echo "$MESSAGE" | mail -s "$SUBJECT" $EMAIL
            echo $SUBJECT
            echo "Alert sent to $EMAIL"
        done
    fi
  fi
done < $SITESFILE
#/bin/bash
SITESFILE=/sites.txt#在此文件中列出要监视的站点
EMAILS=”“#接收警报的电子邮件地址列表(逗号分隔)
同时阅读网站;做
如果[!-z“${site}”];然后
CURL=$(CURL-s--head$站点)
如果echo$CURL | grep“200 OK”>/dev/null
然后
echo“${site}上的HTTP服务器已启动!”
睡眠2
其他的
MESSAGE=“这是一个警报,表明您的站点${site}未能响应200 OK。”
对于美元形式的电子邮件(echo$电子邮件| tr“,”);做
SUBJECT=“$site(http)失败”
回显“$MESSAGE”|邮件-s“$SUBJECT”$电子邮件
echo$主题
回显“发送到$EMAIL的警报”
完成
fi
fi
完成<$SITESFILE

是,
男子卷发

--connect-timeout <seconds>
    Maximum  time  in seconds that you allow the connection to the server to take.
    This only limits the connection phase, once curl has connected this option is 
    of no more use. See also the -m, --max-time option.
--连接超时
允许连接到服务器的最长时间(秒)。
这只会限制连接阶段,一旦curl已连接,则此选项无效
不再有用了。另请参见-m,-max time选项。

在调用CURL之前,还可以考虑使用<代码> ping <代码>来测试连接。带有

ping-c2
的东西将为您提供2次ping来测试连接。然后只需检查ping的返回(即
[[[$?-eq 0]]
表示ping成功,然后连接curl)


您还可以使用
[-n${site}]
(站点已设置)而不是
[!-z${site}]
(站点未取消设置)。此外,对于测试构造,您通常希望使用
[[]]
测试关键字,而不是单个
[]
。为了实现最终的可移植性,只需使用
test-n“${site}”
(在使用
test

时始终使用双引号,我认为您需要此选项
--最大时间

-m/--最大时间
允许整个操作花费的最长时间(秒)。这有助于防止批处理作业挂起数小时
由于网络或链路运行缓慢。
--连接超时
允许连接到服务器的最长时间(以秒为单位)。这仅限制连接阶段,一旦curl连接,此选项将不再使用。
-m/--max-time <seconds>
          Maximum time in seconds that you allow the whole operation to take.  This is useful for preventing your batch jobs from hanging for hours
          due to slow networks or links going down. 

--connect-timeout <seconds>
          Maximum  time  in seconds that you allow the connection to the server to take.  This only limits the connection phase, once curl has connected this option is of no more use.