-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.sh
executable file
·150 lines (129 loc) · 2.95 KB
/
control.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/sh
if [ ''$USER == 'root' ]; then
echo "This script must not be run as root" 1>&2
exit 1
fi
# $1: start/stop/watch
# $2: [nn]
# default values
port=8080
httpsPort=8443
stopkey=secret-default
runlog=logs/runlog
# make logs directory of not present
if [ ! -d logs ]; then
mkdir logs
fi
# read defaults from file
if [ -f .defaults ]; then
. .defaults
fi
# read command line argument if present
if [ -n "$2" ]; then
node=$2
fi
# node with value "01"-"99" to control multinode setup
if [ -n "$node" ] && [ "$node" -gt 0 ] && [ "$node" -lt 100 ] ; then
# prefix with "0"
node=`printf "%02i" $node`
port=90$node
httpsPort=91$node
stopkey=secret$node
runlog=${runlog}-$node
fi
# look for jar file to start
jarfile=`ls -1S *jar 2>/dev/null|head -1`
if [ ''$jarfile == '' ]; then
echo "Could not find jar file" 1>&2
exit 1
fi
# export variables for java
JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"
JAVA_OPTS="$JAVA_OPTS -Djava.io.tmpdir=/tmp"
JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"
JAVA_OPTS="$JAVA_OPTS -Djetty.port=${port}"
JAVA_OPTS="$JAVA_OPTS -Dhttps.port=${httpsPort}"
JAVA_OPTS="$JAVA_OPTS -DSTOP.KEY=${stopkey}"
export JAVA_OPTS
start() {
echo -n "Starting $jarfile on port $port "
java $JAVA_OPTS -jar $jarfile >> $runlog 2>&1 &
# wait for it to start, then install watchdog
url=http://127.0.0.1:${port}/
while true; do
echo -n .
curl -s $url > /dev/null && install && break
sleep 2
done
echo
}
stop() {
echo -n "Stopping $jarfile on port $port "
# first, uninstall watchdog
uninstall
# then stop jetty
n=0
while true; do
pid=`ps auxww|grep $stopkey|grep -v grep|awk '{print $2}'`
if [ $pid ]; then
echo -n .
if [ $n -lt 2 ]; then
kill $pid
fi
if [ $n -gt 10 ]; then
lsof -p $pid > logs/lsof-$pid 2>&1
jstack -l $pid > logs/jstack-$pid 2>&1
kill -9 $pid
fi
n=$(($n+1))
sleep 1
else
break
fi
done
echo
}
restart() {
stop
start
}
watch() {
url=http://127.0.0.1:${port}/
# look for reason to restart
unset reason
[ -f $runlog ] && tail -100 $runlog | grep OutOfMemoryError && reason=memory
[ -n "$reason" ] || curl -s --connect-timeout 10 --max-time 10 $url > /dev/null || reason=http
# restart if there is a reason for it
if [ -n "$reason" ]
then
echo -n "restart node ${node} for reason ${reason} " ; date
restart
fi
}
install() {
crontab -l 2>/dev/null | grep -v "watch $node" > crontab
dir=$(cd $(dirname $0) ; pwd)
echo "* * * * * cd $dir && bash control.sh watch $node >> logs/watchdog.log 2>&1 " >> crontab
crontab crontab
rm crontab
}
uninstall() {
crontab -l | grep -v "watch $node" | crontab -
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
watch)
watch
;;
*)
echo "Usage: $0 {start|stop|restart|watch} [01-99]"
;;
esac