forked from Moekr/moocoder-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.sh
More file actions
130 lines (118 loc) · 2.93 KB
/
app.sh
File metadata and controls
130 lines (118 loc) · 2.93 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
#!/usr/bin/env bash
# Application root directory.
path=/srv/moocoder
# Application username.
name=moocoder-backend
# Application version.
version=0.4.5
# Application config file. Default config.yml
config_file=${path}/config.yml
# File which store application process PID. Default proc.pid
pid_file=${path}/proc.pid
# Directory which store application log. Default ${path}/log
log_dir=${path}/log
# Name template of log files (Parsed with shell command "date"). Default %Y-%m-%d_%H-%M-%S.log
log_name=%Y-%m-%d_%H-%M-%S.log
# Time in second how long "stop" and "force-stop" wait for application totally stop. Default 60
wait_count=60
# JVM default time zone. Default GMT+8
time_zone="GMT+8"
function start {
if [ -f ${pid_file} ]
then
local pid=$(cat ${pid_file})
if ps -p ${pid} > /dev/null
then
echo "Application ${name} is already running! (Version:${version} PID:${pid})"
else
rm -rf ${pid_file}
fi
fi
if [ ! -f ${pid_file} ]
then
if [ ! -d ${log_dir} ]
then
mkdir ${log_dir}
fi
java -server -jar ${path}/${name}-${version}.jar --spring.config.additional-location=${config_file} -Duser.timezone=${time_zone} &>${log_dir}/$(date +${log_name}) &
local pid=$(echo -e "$!\c")
echo -e "${pid}\c" > ${pid_file}
echo "Start application ${name} successfully! (Version:${version} PID:${pid})"
fi
}
function stop {
if [ -f ${pid_file} ]
then
if ! ps -p $(cat ${pid_file}) > /dev/null
then
rm -rf ${pid_file}
fi
fi
if [ -f ${pid_file} ]
then
local pid=$(cat ${pid_file})
kill ${pid}
local count=${wait_count}
while((${count} > 0))
do
sleep 1
if ! ps -p ${pid} > /dev/null
then
rm -rf ${pid_file}
echo "Stop application ${name} successfully! (Version:${version})"
break
fi
count=`expr ${count} - 1`
done
if [ ${count} -eq 0 ]
then
echo "Failed to stop application ${name}! (Version:${version} PID:${pid})"
exit 1
fi
else
echo "Application ${name} is not running! (Version:${version})"
fi
}
function restart {
stop
start
}
function status {
if [ -f ${pid_file} ]
then
if ! ps -p $(cat ${pid_file}) > /dev/null
then
rm -rf ${pid_file}
fi
fi
if [ -f ${pid_file} ]
then
local pid=$(cat ${pid_file})
echo "Application ${name} is running! (Version:${version} PID:${pid})"
else
echo "Application ${name} is not running! (Version:${version})"
fi
}
function usage {
echo "Usage: $0 {start|stop|restart|status}"
}
pushd ${path} > /dev/null
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
usage
;;
esac
popd > /dev/null
exit 0