-
Notifications
You must be signed in to change notification settings - Fork 74
/
init.sh
executable file
·153 lines (146 loc) · 4.23 KB
/
init.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
151
152
153
#!/bin/bash
#
# Copyright 2016 Palantir Technologies
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# <http://www.apache.org/licenses/LICENSE-2.0>
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
is_process_active() {
local PID=$1
ps $PID > /dev/null;
echo $?
}
# Everything in this script is relative to the base directory of an SLSv2 distribution
pushd "`dirname \"$0\"`/../.." > /dev/null
# Select launcher binary for this OS
case "`uname`" in
Linux*)
LAUNCHER_CMD=service/bin/linux-amd64/go-java-launcher
;;
Darwin*)
LAUNCHER_CMD=service/bin/darwin-amd64/go-java-launcher
;;
*)
echo "Unsupported operating system: $(uname)"; exit 1
esac
ACTION=$1
SERVICE="@serviceName@"
PIDFILE="var/run/$SERVICE.pid"
STATIC_LAUNCHER_CONFIG="service/bin/launcher-static.yml"
CUSTOM_LAUNCHER_CONFIG="var/conf/launcher-custom.yml"
STATIC_LAUNCHER_CHECK_CONFIG="service/bin/launcher-check.yml"
case $ACTION in
start)
service/bin/init.sh status > /dev/null 2>&1
if [[ $? == 0 ]]; then
echo "Process is already running"
exit 0
fi
printf "%-50s" "Running '$SERVICE'..."
# ensure log and pid directories exist
mkdir -p "var/log"
mkdir -p "var/run"
PID=$($LAUNCHER_CMD $STATIC_LAUNCHER_CONFIG $CUSTOM_LAUNCHER_CONFIG > var/log/$SERVICE-startup.log 2>&1 & echo $!)
sleep 1
if [ $(is_process_active $PID) -eq 0 ]; then
echo $PID > $PIDFILE
printf "%s\n" "Started ($PID)"
exit 0
else
printf "%s\n" "Failed"
exit 1
fi
;;
status)
printf "%-50s" "Checking '$SERVICE'..."
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [[ $(is_process_active $PID) -eq 0 ]]; then
ps -o command $PID | grep -q "$SERVICE"
if [[ $? == 0 ]]; then
printf "%s\n" "Running ($PID)"
exit 0
else
printf "%s\n" "Warning, Pid $PID appears to not correspond to service $SERVICE"
exit 0
fi
fi
printf "%s\n" "Process dead but pidfile exists"
exit 1
else
printf "%s\n" "Service not running"
exit 3
fi
;;
stop)
printf "%-50s" "Stopping '$SERVICE'..."
service/bin/init.sh status > /dev/null 2>&1
if [[ $? == 0 ]]; then
PID=$(cat $PIDFILE)
kill $PID
COUNTER=0
while [ $(is_process_active $PID) -eq "0" -a "$COUNTER" -lt "240" ]; do
sleep 1
let COUNTER=COUNTER+1
if [ $((COUNTER%5)) == 0 ]; then
if [ "$COUNTER" -eq "5" ]; then
printf "\n" # first time get a new line to get off Stopping printf
fi
printf "%s\n" "Waiting for '$SERVICE' ($PID) to stop"
fi
done
if [[ $(is_process_active $PID) -eq "0" ]]; then
printf "%s\n" "Failed"
exit 1
else
rm -f $PIDFILE
printf "%s\n" "Stopped ($PID)"
exit 0
fi
else
rm -f $PIDFILE
printf "%s\n" "Service not running"
exit 0
fi
;;
console)
service/bin/init.sh status > /dev/null 2>&1
if [[ $? == 0 ]]; then
echo "Process is already running"
exit 1
fi
trap "service/bin/init.sh stop &> /dev/null" SIGTERM EXIT
mkdir -p "$(dirname $PIDFILE)"
$LAUNCHER_CMD $STATIC_LAUNCHER_CONFIG $CUSTOM_LAUNCHER_CONFIG &
echo $! > $PIDFILE
wait
;;
restart)
service/bin/init.sh stop
service/bin/init.sh start
;;
check)
printf "%-50s" "Checking health of '$SERVICE'..."
$LAUNCHER_CMD $STATIC_LAUNCHER_CHECK_CONFIG > var/log/$SERVICE-check.log 2>&1
RESULT=$?
if [ $RESULT -eq 0 ]; then
printf "%s\n" "Healthy"
exit 0
else
printf "%s\n" "Unhealthy"
exit $RESULT
fi
;;
*)
echo "Usage: $0 {status|start|stop|console|restart|check}"
exit 1
esac