/
cst-sparql-safe-run.sh
76 lines (49 loc) · 1.52 KB
/
cst-sparql-safe-run.sh
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
#!/bin/bash
#
# Executes the given command and attempts to re run it on any other exit code than 0
#
# Usage:
# safe-run.sh command
#
limit=45
progCmd="$1"
#testCmd="$2"
sparqlServiceUri="$2"
testQuery='Select%20%2A%20%7B%20%3Chttp%3A%2F%2Fex.org%3E%20%3Chttp%3A%2F%2Fex.org%3E%20%3Chttp%3A%2F%2Fex.org%3E%20%7D%20Limit%201'
testCmd="curl --connect-timeout 30 -m 30 -o /dev/null --silent --head --write-out %{http_code} $sparqlServiceUri?query=$testQuery"
#echo "$testCmd"
#exit 1
#curl http://dbpedia.org/sparql -o /dev/null --silent --head --write-out '%{http_code}'
while true; do
echo "Performing safe run of the SPARQL endpoint $sparqlServiceUri"
startTime=`date +%s`
$progCmd &
pid="$!"
echo "Process ID is: $pid"
while true; do
endTime=`date +%s`
delta=$((endTime-startTime))
delay=$((limit-delta))
if [ $delay -lt 0 ]; then
delay="0"
fi
echo "Next test in: $delay seconds"
sleep "$delay"
httpMsg=`$testCmd`
exitCode="$?"
echo "Message: $httpMsg"
httpCode=`echo "$httpMsg" | awk '{ print $1 }'`
echo "Check result: Exit Code: $exitCode, HTTP Status Code: $httpCode"
if (( exitCode > 0 || httpCode != 200 )); then
echo "Problem Encountered. Killing the service..."
#kill "$pid"
pkill -TERM -P "$pid"
sleep 15
echo "Forcing kill just in case"
pkill -KILL -P "$pid"
sleep 15
break
fi
startTime=`date +%s`
done
done