-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·157 lines (126 loc) · 2.55 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·157 lines (126 loc) · 2.55 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
#!/bin/bash
VERSION=1.0
# Default location of apps
LOCATION="$HOME/apps"
# Usage message
usage() {
cat <<EOF
Usage:
-v | --version get version
-h | --help print help
-n | --name specify app name
-a | --action specify action
-e | --extra-args extra command line args to be passed
List of available actions:
start starts application, fails gracefully if already running
stop stops application, fails gracefully if not running
status prints current application status
EOF
}
# Lazy evaluated properties
app_root() {
echo $LOCATION/$APP_NAME
}
pid_file() {
echo $(app_root)/app.pid
}
app_file() {
echo $(app_root)/$APP_NAME.jar
}
std_err_file() {
echo $(app_root)/std.err
}
pid() {
cat $(pid_file)
}
is_running() {
if [ -e "$(pid_file)" ]; then
[ $(ps -e -o pid | grep -Eo "^\s+$(pid)\$") ] && return 0
rm -f "$(pid_file)"
fi
return 1
}
start() {
if is_running; then
echo $APP_NAME is already running with PID $(pid)
return 0
fi
if [ ! -e "$(app_file)" ]; then
echo application "$(app_file)" not found
return 1
fi
nohup java "${EXTRA_ARGS[@]}" -jar "$(app_file)" >/dev/null 2>$(std_err_file) &
echo $! > "$(pid_file)"
if is_running; then
echo $APP_NAME successfully started
return 0
else
echo FAILED to start $APP_NAME
return 1
fi
}
stop() {
if ! is_running; then
echo $APP_NAME is not running
return 0
fi
kill "$(pid)"
sleep 5
if ! is_running; then
echo successfully stopped $APP_NAME
return 0
else
echo FAILED to stop $APP_NAME. It may still be running.
return 1
fi
echo stop
}
status() {
if is_running; then
echo $APP_NAME is currently RUNNING with PID $(pid)
else
echo $APP_NAME is currently NOT RUNNING
fi
}
# Handle input
while [ "$#" -gt 0 ]; do
case "$1" in
-v | --version )
echo $VERSION
exit 0
;;
-h | --help )
echo Meckeys JAR deployer - $VERSION
usage
exit 0
;;
-n | --name )
APP_NAME="$2"
shift
;;
-a | --action )
ACTION="$2"
shift
;;
-e | --extra-args )
EXTRA_ARGS+=("$2")
shift
;;
* )
echo Invalid argument "$1"
exit 1
esac
shift
done
if [ "${APP_NAME+x}${ACTION+y}" != 'xy' ]; then
echo '-n | --name and -a | --action are required'
exit 1
fi
# Parse action
if [[ "start stop status" =~ "$ACTION" ]]; then
"$ACTION"
exit $?
else
echo invalid action $ACTION
exit 1
fi