-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhttp-status-check
executable file
·77 lines (67 loc) · 2.23 KB
/
http-status-check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# This shell script takes a url and an optional timeout period (the default being 30 seconds) and tests to see if the URL is live
# If there is no response immediately, this script retries every 5 seconds until a response is obtained or until the timeout period is reached.
# For this reason, this script is ideal for testing that a tomcat instances started up as expected: send a "start" command,
# then run this script which will wait whilst tomcat initializes.
# This script will return zero if the URL exists, and print a message containing the http response,
# or will return the curl exit code if the URL could not be accessed.
#
# Tony Burdett, Jan 2012
printUsage() {
printf "Usage: %s: [-u URL] [-t timeout] args\n" $0;
exit 2;
}
# -u arg is required (URL to ping), -t is optional to change default timeout
while getopts u:t: name
do
case $name in
u) url="$OPTARG";;
t) timeout="$OPTARG";;
?) printUsage $0;;
esac
done
# check which args are supplied
if [ -z "$url" ];
then
printf "Required URL argument -u not specified\n";
exit 2;
fi
if [ -z "$timeout" ];
then
timeout=30;
printf "Timeout not supplied, set to default 30 s.\n";
else
printf "Timeout set to $timeout s.\n";
fi
# retry every 5 seconds for some length of time, $timeout
retries=`expr $timeout / 5`;
# issue a curl command, -L follows redirects, --silent --output swallows any output, writes out http response code only
cmd="curl --max-time 120 --write-out %{http_code} --silent --output /dev/null -L $url"
printf "Testing status of '$url'...";
httpStatus="`$cmd`";
response=$?;
count=0;
# retry while we get a curl exit code of 7 (couldn't connect) or a non-200 http status, for max number of retries
while [[ ( $response = 7 || $httpStatus != 200 ) && $count < $retries ]]
do
printf ".";
count=$((count+1));
sleep 5;
httpStatus="`$cmd`";
response=$?;
done
# check exit - if curl gave a response then we're done...
if [ $response -eq 0 ]
then
printf "complete!\n";
printf "HTTP RESPONSE: $httpStatus\n";
# ... but only exit with 0 if http response was 200
if [ $httpStatus -ne 200 ]
then
response=2;
fi
else
printf "failed, exited with exit code $response\n";
fi
# exit with curl response code, except where we got bad http response (then 2)
exit $response;