/
runservice
executable file
·222 lines (206 loc) · 6.27 KB
/
runservice
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash
################################################################################
# Required: run this script from the project home directory
#
# Guide: to be able use this script, project should be built by ant or maven
# , so that it will pack a manifest info for execution into distrib jar file
# To build a netbeans project with ant, go to project home directory (location of build.xml)
# , then run "ant jar" command (may run "ant clean" for clean the previous build before)
#
################################################################################
#
# Common options need to change: APP_NAME, APP_VER
#
# $1: Service action: try/start/stop/restart/status
# $2: Application profile: production/development (default is production)
#
#
ENTRY_PATH=`readlink -f "$0"`
PROJECT_HOME=`dirname "$ENTRY_PATH"`
CMD_DIR=cmd
CONF_DIR=conf
JAR_DIR=dist
#DEPENDENT_JARS="$PROJECT_HOME"/dist/lib1/*:"$PROJECT_HOME"/dist/lib2/*: #java will search jar files defined here and ones defined in the class path of the main jar, still error with jetty, will fix later...
#MAIN_CLASS="com.vng.zing.testapp.app.MainApp"
cd "$PROJECT_HOME"
#setup JAVA environment
. "$PROJECT_HOME"/$CMD_DIR/_sys-env.sh
################################################################################
#setup Application environment
APP_NAME=TestApp
APP_VER=""
#JAR_NAME= #define if JAR_NAME is a customized name which is not based on APP_NAME & APP_VER
################################################################################
#do work
#
#name of jar
if [ "x$JAR_NAME" = "x" ]; then
if [ "x$APP_VER" != "x" ]; then
JAR_NAME="$APP_NAME-$APP_VER"
else
JAR_NAME="$APP_NAME"
fi
fi
TMP_DIR="/zserver/tmp/$APP_NAME"
LOG_DIR="/data/log/$APP_NAME"
if [ "x$2" != "x" ]; then
APP_PROF="$2"
else
APP_PROF="production"
fi
#pid file
PID_FILE="$APP_NAME.pid"
if [ "x$PID_FILE" != "x" ]; then
PID_PATH="$TMP_DIR/$PID_FILE"
fi
#run-log file
RUNLOG_FILE="$APP_NAME.log"
if [ "x$RUNLOG_FILE" != "x" ]; then
RUNLOG_PATH="$TMP_DIR/$RUNLOG_FILE"
fi
testLaunchService() {
########## inline setup ##########
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Setup Info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " * setup run-arguments from: $PROJECT_HOME/$CMD_DIR/$APP_PROF-service-env.sh"
. "$PROJECT_HOME"/$CMD_DIR/_pre-service-env.sh
. "$PROJECT_HOME"/$CMD_DIR/$APP_PROF-service-env.sh
. "$PROJECT_HOME"/$CMD_DIR/_post-service-env.sh
echo " * app configuration will be loaded from: $PROJECT_HOME/$CONF_DIR/$APP_PROF.$CONF_FILES"
echo
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Execution Info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
########## prepare ##########
if [ "x$DEPENDENT_JARS" = "x" ]; then
if [ "x$MAIN_CLASS" = "x" ]; then
RUN_CMD=(${JAVA:-java} $JVM_ARGS -jar "$PROJECT_HOME/$JAR_DIR/$JAR_NAME.jar" $APP_ARGS)
else
RUN_CMD=(${JAVA:-java} $JVM_ARGS -cp "$PROJECT_HOME/$JAR_DIR/$JAR_NAME.jar" $MAIN_CLASS $APP_ARGS)
fi
else
if [ "x$MAIN_CLASS" = "x" ]; then # get main class in main jar if not specify
MAIN_CLASS=`unzip -q -c "$PROJECT_HOME/$JAR_DIR/$JAR_NAME.jar" META-INF/MANIFEST.MF | grep "Main-Class" | awk '{print $2}'`
MAIN_CLASS="${MAIN_CLASS%?}" # remove last unprintable char
fi
RUN_CMD=(${JAVA:-java} $JVM_ARGS -cp "$PROJECT_HOME/$JAR_DIR/$JAR_NAME.jar:$DEPENDENT_JARS" $MAIN_CLASS $APP_ARGS)
fi
echo Run command: "${RUN_CMD[@]}"
echo Console log: $RUNLOG_PATH
mkdir -p $TMP_DIR
}
launchService() {
testLaunchService
########## execute ##########
"${RUN_CMD[@]}" 1>>"$RUNLOG_PATH" 2>>"$RUNLOG_PATH" &
/bin/echo -n $! > "$PID_PATH"
}
checkService() {
if [ -e "$PID_PATH" ]; then
_PID="`cat $PID_PATH`"
_PINF="`ps -fp $_PID | grep $_PID`"
if [ "x$_PINF" = "x" ]; then
rm -f "$PID_PATH"
fi
fi
}
cleanLog() {
echo "Cleaning up: $TMP_DIR ..."
rm -f $TMP_DIR/*.log
echo "Cleaning up: $LOG_DIR ..."
rm -f $LOG_DIR/*.log
rm -f $LOG_DIR/*.log.*
}
printStatus() {
if [ -e "$PID_PATH" ]; then
echo "Application is running!"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Process Info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
ps -fp `cat $PID_PATH` | grep ""
#lsof -p `cat $PID_PATH` | grep "/zserver/lib/zx"
else
echo "Application stopped!"
fi
}
printUsage() {
echo "Usage: `basename "$ENTRY_PATH"` try|start|stop|restart|status|sysinfo|cll [production|development]"
echo
echo " The first option is service action:"
echo " - try: print out arguments & environment for start program, the program will not be launched"
echo " - start: launch the program"
echo " - stop: kill the program"
echo " - restart: kill the program first, then launch again the program"
echo " - status: show the program is running or stopped"
echo " - sysinfo: print out the system info"
echo " - cll: clean log files of the program"
echo " - help: print this guide"
echo
echo " The second option is application's profile:"
echo " - production(default): will load profile of production (commonly defined in \"$CMD_DIR/production-service-env.sh\")"
echo " - development: will load profile of development (commonly defined in \"$CMD_DIR/development-service-env.sh\")"
}
printSysInfo() {
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ System Info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "To change the JAVA, export JAVA_NAME before running this script $0, ex export JAVA_NAME=\"jdk1.7.0_51-x64\""
echo "Java Home $JAVA_HOME"
echo "Java: $JAVA"
echo "Jvm Vendor: $JVM_VENDOR"
echo "Jvm Arch: $JVM_ARCH"
echo "Jvm Version: $JVM_VERSION""_$JVM_PATCH_VERSION"
echo "Suggested max heap size for setting (xmx): $MAX_HEAP_SIZE"
echo "Suggested new heap size for setting (xmn) (not for G1 gc): $HEAP_NEWSIZE"
}
checkService
case "$1" in
try)
#"$ENTRY_PATH" status $APP_PROF
printSysInfo
echo
testLaunchService
echo
;;
start)
if [ -e "$PID_PATH" ]; then
echo "Application is already running!"
echo
exit 1
fi
##ok:start it
printSysInfo
echo
launchService
echo
"$ENTRY_PATH" status $APP_PROF
;;
stop)
if [ ! -e "$PID_PATH" ]; then
echo "Application already stopped!"
echo
exit 1
fi
##ok:stop it
kill -9 `cat $PID_PATH`
rm $PID_PATH -f
echo "Stopped."
echo
;;
restart)
"$ENTRY_PATH" stop $APP_PROF
echo "Waiting..."
##forced to stop above, so it doesn't need a long sleep
sleep 2
"$ENTRY_PATH" start $APP_PROF
;;
status)
printStatus
echo
;;
sysinfo)
printSysInfo
echo
;;
cll)
cleanLog
echo
;;
*)
printUsage
echo
exit 1
esac