Malinda Peiris
Malinda Peiris

Reputation: 614

Time out a curl command shell script centOS

checkServer(){
    response=$(curl --connect-timeout 10  --write-out %{http_code} --silent --output /dev/null localhost:8080/patuna/servicecheck)

    if [ "$response" = "200" ];
        then echo "`date --rfc-3339=seconds` -  Server is healthy, up and running"
        return 0
    else
        echo "`date --rfc-3339=seconds` -  Server is not healthy(response code - $response ), server is going to restrat"
        startTomcat
    fi
}

Here i want to time out the curl command but it dose not work. in centos7 Shell scrit. what i simply needs to do is timeout the curl command ERROR code is curl: option --connect-timeout=: is unknown

Upvotes: 1

Views: 2485

Answers (2)

Dongfang Qu
Dongfang Qu

Reputation: 351

You can try the option of --max-time.

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. Since 7.32.0, this option accepts decimal values, but the actual timeout will decrease in accuracy as the specified timeout increases in decimal precision.

If you just want to check the http status code. You might want to check out the --head option.

I suggest using --silent with --show-error at the same time in case that you might want to know the error message.

Upvotes: 1

Malinda Peiris
Malinda Peiris

Reputation: 614

checkServer(){
        response=$(curl --max-time 20 --connect-timeout 0  --write-out %{http_code} --silent --output /dev/null localhost:8080/patuna/servicecheck)

        if [ "$response" = "200" ];
           then echo "`date --rfc-3339=seconds` -  Server is healthy, up and running"
           return 0
        else
           echo "`date --rfc-3339=seconds` -  Server is not healthy(response code - $response ), server is going to restrat"
           startTomcat
        fi
}

Upvotes: 1

Related Questions