shell简单检查URL

#!/bin/sh

. /etc/init.d/functions

function checkURL()
{
    checkUrl=$1
    echo 'check url start ...'
    judge=($(curl -I -s --connect-timeout 2 ${checkUrl}|head -1|tr "\r" "\n"))
    if [ "${judge[1]}" == '200' -a "${judge[2]}" == 'OK' ]    #判断http状态码且字符为OK
        then
            action "${checkUrl}" /bin/true
        else
            action "${checkUrl}" /bin/false

            echo -n "retrying again...";sleep 3;

            judgeagain=($(curl -I -s --connect-timeout 2 ${checkUrl}|head -1|tr "\r" "\n"))
            if [ "${judgeagain[1]}" == '200' -a "${judgeagain[2]}"=='OK' ]
            then
                action "${checkUrl},retried again" /bin/true
            else
                action "${checkUrl},retried again" /bin/false
            fi
        fi
        sleep 1;
}
#usage method
checkURL www.baidu.com

posted @ 2021-05-16 11:10  东方无解  阅读(354)  评论(0编辑  收藏  举报