-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsvc-service
More file actions
executable file
·171 lines (153 loc) · 3.29 KB
/
jsvc-service
File metadata and controls
executable file
·171 lines (153 loc) · 3.29 KB
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
#!/bin/bash
set -eu
param=${1-undef}
# http://commons.apache.org/proper/commons-daemon/
# path to jsvc must be absolute
jsvc=${HOME}/bin/jsvc
export JAVA_HOME="/opt/java/latest"
app_home="${HOME}/git/jsvc-wrapper/target/scala-2.11"
work_dir="${HOME}/git/jsvc-wrapper"
app_name="myJsvcDemoApp"
jar=$app_home/jsvc-demo-app.jar
main_class=JsvcDemoDaemon
std_out_file=${work_dir}/logs/std-out.log
std_err_file=${work_dir}/logs/std-err.log
pid_file="${work_dir}/logs/app.pid"
logger_config_file="${work_dir}/logback.xml"
# do not change below this line
# #############################################################################
run_jsvc() {
if [[ ! -x ${jsvc} ]]; then
error "jsvc executable not found at ${jsvc}"
fi
if [[ ! -f ${jar} ]]; then
error "jar not found at ${jar}"
fi
check_std_log ${std_out_file}
check_std_log ${std_err_file}
# -procname "${app_name}" is the grep key for ps lookup
${jsvc} \
-procname "${app_name}" \
-cwd "${work_dir}" \
-user "${USER}" \
-pidfile "${pid_file}" \
-outfile "${std_out_file}" \
-errfile "${std_err_file}" \
-Dlogback.configurationFile=${logger_config_file} \
-classpath "${jar}" \
$1 ${main_class}
}
error() {
echo "E: ${1}" >&2
exit 1
}
check_std_log() {
logfile=$1
if [[ -f ${logfile} ]]; then
if [[ ! -s ${logfile} ]]; then
rm ${logfile}
else
error "${logfile} must be empty, check your app or configuration"
fi
fi
}
run_jsvc_stop() {
if [[ -f ${pid_file} ]]; then
run_jsvc "-stop"
else
error "no pidfile found try kill manually"
fi
}
is_running() {
verbose=${1-0}
if [[ -f ${pid_file} ]]; then
if [[ ${verbose} = 1 ]]; then
echo "pidfile found"
fi
if do_ps_status $(cat ${pid_file}) ${verbose}; then
return 1
else
return 0
fi
else
if [[ ${verbose} = 1 ]]; then
echo "no pidfile found"
fi
if do_ps_status "" ${verbose}; then
return 1
else
return 0
fi
return 1
fi
}
do_ps_status() {
pid=$1
verbose=${2}
ps_lines=$(ps -ae -o pid,ppid,uname,args \
| egrep --color=yes "\-procnam[e] ${app_name}" || true)
pid_result=$(echo "$ps_lines" | egrep "^[ \t]*${pid}" || true)
if [[ -z $pid_result ]]; then
if [[ ${verbose} = 1 ]]; then
echo "no process is running"
if [[ -f ${pid_file} ]]; then
pid_content="$(cat ${pid_file})"
echo "pidfile content is: ${pid_content}"
rm -vf ${pid_file}
fi
fi
return 0
else
if [[ ${verbose} = 1 ]]; then
echo "is running"
echo -e "$pid_result"
fi
return 1
fi
}
do_start() {
if is_running; then
error "${app_name} is already started"
else
echo -n "starting ${app_name} ... "
run_jsvc ""
sleep 1
if is_running; then
echo "done"
else
echo ""
error "startup failed"
fi
fi
}
do_stop() {
if is_running; then
echo -n "stopping ${app_name} ... "
run_jsvc_stop
echo "done"
else
error "${app_name} is already stoped"
fi
}
case "$param" in
start)
do_start
;;
stop)
do_stop
;;
status)
is_running 1
;;
restart)
echo "restarting ${app_name}:"
if is_running; then
do_stop
fi
do_start
;;
*)
echo "usage: $0 {start|stop|restart|status}" >&2
exit 3
;;
esac