bash脚本| URL健康检查|邮件发送选项

时间:2015-10-14 10:22:45

标签: linux bash shell

我们需要每分钟健康检查一次网址。

bash脚本的算法如下:

if (url is not up)
  send mail
  wait for 5 minutes 
  if (still not up)
    send mail again and keep on doing it 
else
  nothing

我的逻辑是:

#!/bin/bash
IP="192.168.XX.XXX"
URL="http://$IP:YYYY"
CCMAIL="abc@def.com"
TOMAIL="abc@def.com"
STATUSCODE=$(curl -o /dev/null --silent --head --write-out '%{http_code}\n' $URL)
NOWENTRY=$(date +"%d-%m-%Y  %r")
NOWLOG=$(date +"%d-%m-%Y")

if [ $STATUSCODE -ne "200" ]
then
        echo "$IP URL: $URL was down at $NOWENTRY" >> "path/log_$NOWLOG.txt"
        #send mail in one line no timing logic out here   
fi

注意:我已将此脚本放在crontab中以执行每一分钟的时间。

1 个答案:

答案 0 :(得分:0)

if [ "$STATUSCODE" -ne "200" ]
then
    msg="$IP URL: $URL was down at $NOWENTRY"
    echo "$msg" >> "path/log_$NOWLOG.txt"
    # send email
    echo "$msg" | mailx -c "$CCEMAIL" -s "$URL down" "$TOEMAIL"
    sleep 5m
    # re-spawn this script.
    exec "$0"
fi