-
Notifications
You must be signed in to change notification settings - Fork 55
/
startup.sh
137 lines (120 loc) · 3.26 KB
/
startup.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
#!/bin/sh
#
# birdname init.d script.
#
# All java services require the same directory structure:
# /usr/local/$APP_NAME
# /var/log/$APP_NAME
# /var/run/$APP_NAME
APP_NAME="birdname"
ADMIN_PORT="9900"
VERSION="@VERSION@"
APP_HOME="/usr/local/$APP_NAME/current"
DAEMON="/usr/local/bin/daemon"
JAR_NAME="$APP_NAME-$VERSION.jar"
STAGE="production"
HEAP_OPTS="-Xmx4096m -Xms4096m -XX:NewSize=768m"
GC_OPTS="-XX:+UseParallelOldGC -XX:+UseAdaptiveSizePolicy -XX:MaxGCPauseMillis=1000 -XX:GCTimeRatio=99"
GC_LOG_OPTS="-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC"
GC_LOG="-Xloggc:/var/log/$APP_NAME/gc.log"
DEBUG_OPTS="-XX:ErrorFile=/var/log/$APP_NAME/java_error%p.log"
JAVA_OPTS="-server -Dstage=$STAGE $GC_OPTS $GC_LOG_OPTS $GC_LOG $HEAP_OPTS $DEBUG_OPTS"
pidfile="/var/run/$APP_NAME/$APP_NAME.pid"
daemon_pidfile="/var/run/$APP_NAME/$APP_NAME-daemon.pid"
daemon_args="--name $APP_NAME --pidfile $daemon_pidfile --core --chdir /"
daemon_start_args="--stdout=/var/log/$APP_NAME/stdout --stderr=/var/log/$APP_NAME/error"
function running() {
$DAEMON $daemon_args --running
}
function find_java() {
if [ ! -z "$JAVA_HOME" ]; then
return
fi
for dir in /opt/jdk /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home /usr/java/default; do
if [ -x $dir/bin/java ]; then
JAVA_HOME=$dir
break
fi
done
}
find_java
case "$1" in
start)
echo -n "Starting $APP_NAME... "
if [ ! -r $APP_HOME/$JAR_NAME ]; then
echo "FAIL"
echo "*** $APP_NAME jar missing: $APP_HOME/$JAR_NAME - not starting"
exit 1
fi
if [ ! -x $JAVA_HOME/bin/java ]; then
echo "FAIL"
echo "*** $JAVA_HOME/bin/java doesn't exist -- check JAVA_HOME?"
exit 1
fi
if running; then
echo "already running."
exit 0
fi
ulimit -c unlimited || echo -n " (no coredump)"
$DAEMON $daemon_args $daemon_start_args -- sh -c "echo "'$$'" > $pidfile; exec ${JAVA_HOME}/bin/java ${JAVA_OPTS} -jar ${APP_HOME}/${JAR_NAME}"
tries=0
while ! running; do
tries=$((tries + 1))
if [ $tries -ge 5 ]; then
echo "FAIL"
exit 1
fi
sleep 1
done
echo "done."
;;
stop)
echo -n "Stopping $APP_NAME... "
if ! running; then
echo "wasn't running."
exit 0
fi
curl -m 5 -s http://localhost:${ADMIN_PORT}/shutdown.txt > /dev/null
tries=0
while running; do
tries=$((tries + 1))
if [ $tries -ge 15 ]; then
echo "FAILED SOFT SHUTDOWN, TRYING HARDER"
if [ -f $pidfile ]; then
kill $(cat $pidfile)
else
echo "CAN'T FIND PID, TRY KILL MANUALLY"
exit 1
fi
hardtries=0
while running; do
hardtries=$((hardtries + 1))
if [ $hardtries -ge 5 ]; then
echo "FAILED HARD SHUTDOWN, TRY KILL -9 MANUALLY"
exit 1
fi
sleep 1
done
fi
sleep 1
done
echo "done."
;;
status)
if running; then
echo "$APP_NAME is running."
else
echo "$APP_NAME is NOT running."
fi
;;
restart)
$0 stop
sleep 2
$0 start
;;
*)
echo "Usage: /etc/init.d/${APP_NAME}.sh {start|stop|restart|status}"
exit 1
;;
esac
exit 0