-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·241 lines (205 loc) · 5.14 KB
/
install.sh
File metadata and controls
executable file
·241 lines (205 loc) · 5.14 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/bin/bash
# JAR Service Manager v1.0
# Usage: ./jsm.sh {start|stop|restart|status|logs}
set -euo pipefail
# Configuration
APP_NAME="bioMedical"
JAR_FILE="/home/hermann90/UTRAINS/devops/github_action/java_app/geoMVPX0/target/bioMedical-1.0.88.jar"
JAVA_OPTS="-Xms512m -Xmx1024m -server"
WORK_DIR="/home/hermann90/UTRAINS/devops/github_action/java_app/geoMVPX0/workdir"
LOG_FILE="${WORK_DIR}/${APP_NAME}.log"
PID_FILE="${WORK_DIR}/${APP_NAME}.pid"
MAX_WAIT=30 # seconds to wait for graceful shutdown
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to display messages
info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check prerequisites
check_prerequisites() {
if ! command -v java &> /dev/null; then
error "Java is not installed or not in PATH"
exit 1
fi
if [[ ! -f "$JAR_FILE" ]]; then
error "JAR file not found: $JAR_FILE"
exit 1
fi
if [[ ! -d "$WORK_DIR" ]]; then
warn "Working directory does not exist, creating..."
mkdir -p "$WORK_DIR"
fi
}
# Check if application is running
is_running() {
if [[ -f "$PID_FILE" ]]; then
PID=$(cat "$PID_FILE")
if kill -0 $PID 2>/dev/null; then
return 0 # Running
else
# Dead process but PID file remains
rm -f "$PID_FILE"
return 1
fi
fi
return 1
}
# Start the application
start_app() {
check_prerequisites
if is_running; then
warn "Application is already running (PID: $(cat "$PID_FILE"))"
return 1
fi
info "Starting $APP_NAME..."
# Execute in background and capture PID
cd "$WORK_DIR"
nohup java $JAVA_OPTS -jar "$JAR_FILE" >> "$LOG_FILE" 2>&1 &
PID=$!
# Save PID
echo $PID > "$PID_FILE"
# Wait a bit to verify the process is still running
sleep 2
if kill -0 $PID 2>/dev/null; then
info "Application started successfully (PID: $PID)"
info "Logs: $LOG_FILE"
return 0
else
error "Failed to start application, check logs: $LOG_FILE"
rm -f "$PID_FILE"
return 1
fi
}
# Stop the application gracefully
stop_app() {
if ! is_running; then
warn "Application is not running"
return 0
fi
PID=$(cat "$PID_FILE")
info "Stopping application (PID: $PID)..."
# Send SIGTERM (graceful shutdown)
kill -TERM $PID 2>/dev/null || true
# Wait for graceful shutdown
local count=0
while is_running && [ $count -lt $MAX_WAIT ]; do
sleep 1
count=$((count + 1))
echo -n "."
done
echo ""
if is_running; then
warn "Graceful shutdown failed after $MAX_WAIT seconds, forcing termination..."
kill -KILL $PID 2>/dev/null || true
sleep 2
fi
if is_running; then
error "Unable to stop application (PID: $PID)"
return 1
else
rm -f "$PID_FILE"
info "Application stopped successfully"
return 0
fi
}
# Restart the application
restart_app() {
stop_app
sleep 2
start_app
}
# Display status
status_app() {
if is_running; then
PID=$(cat "$PID_FILE")
info "Status: ${GREEN}RUNNING${NC} (PID: $PID)"
# Additional process information
if command -v ps &> /dev/null; then
echo "Process information:"
ps -p $PID -o pid,ppid,%cpu,%mem,cmd,huser
fi
# Log file size
if [[ -f "$LOG_FILE" ]]; then
LOG_SIZE=$(du -h "$LOG_FILE" | cut -f1)
echo "Log size: $LOG_SIZE"
fi
return 0
else
info "Status: ${RED}STOPPED${NC}"
return 1
fi
}
# Display logs
show_logs() {
if [[ ! -f "$LOG_FILE" ]]; then
error "Log file not found: $LOG_FILE"
return 1
fi
local lines=50
if [[ $# -eq 1 ]]; then
lines=$1
fi
echo "=== Last $lines lines of log ==="
tail -n $lines "$LOG_FILE"
if is_running; then
echo ""
echo "To follow logs in real-time: tail -f $LOG_FILE"
fi
}
# Display help
show_help() {
cat << EOF
JAR Service Manager - Java JAR Application Manager
Usage: $0 {start|stop|restart|status|logs|help}
Commands:
start Start the application
stop Stop the application gracefully
restart Restart the application
status Display application status
logs [n] Display last n lines of logs (50 by default)
help Display this help message
Configuration in script:
APP_NAME="$APP_NAME"
JAR_FILE="$JAR_FILE"
JAVA_OPTS="$JAVA_OPTS"
WORK_DIR="$WORK_DIR"
LOG_FILE="$LOG_FILE"
EOF
}
# Command handling
case "${1:-}" in
start)
start_app
;;
stop)
stop_app
;;
restart)
restart_app
;;
status)
status_app
;;
logs)
show_logs "${2:-50}"
;;
help|--help|-h)
show_help
;;
*)
error "Invalid command: $1"
echo ""
show_help
exit 1
;;
esac