Repository navigation
Expand file tree
/
Copy pathserver-manager.sh
More file actions
executable file
·301 lines (252 loc) · 8.67 KB
/
Copy pathserver-manager.sh
File metadata and controls
executable file
·301 lines (252 loc) · 8.67 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/bin/bash
# Minecraft Bedrock Server Status and Management Script
# This script provides status information and basic management commands
set -euo pipefail
# Source configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common.sh"
load_config
validate_config || { printf "%s\n" "Unsafe or invalid configuration" >&2; exit 1; }
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Logging function
log() {
local level="$1"
shift
local message="$*"
case "$level" in
INFO) echo -e "${GREEN}[INFO]${NC} $message" ;;
WARN) echo -e "${YELLOW}[WARN]${NC} $message" ;;
ERROR) echo -e "${RED}[ERROR]${NC} $message" ;;
DEBUG) echo -e "${BLUE}[DEBUG]${NC} $message" ;;
HEADER) echo -e "${CYAN}$message${NC}" ;;
esac
}
# Check if server is running
is_server_running() {
has_server_process || has_screen_session
}
# Get server process information
get_server_process_info() {
server_pids | head -1
}
# Show server status
show_server_status() {
log HEADER "=== Minecraft Bedrock Server Status ==="
echo ""
# Check if server is running
if is_server_running; then
log INFO "✓ Server is RUNNING"
# Get process information
local pid=$(get_server_process_info)
if [[ -n "$pid" ]]; then
log INFO "Process ID: $pid"
# Show memory usage
local memory=$(ps -p "$pid" -o rss= 2>/dev/null | tr -d ' ')
if [[ -n "$memory" ]]; then
local memory_mb=$((memory / 1024))
log INFO "Memory usage: ${memory_mb}MB"
fi
# Show CPU usage
local cpu=$(ps -p "$pid" -o %cpu= 2>/dev/null | tr -d ' ')
if [[ -n "$cpu" ]]; then
log INFO "CPU usage: ${cpu}%"
fi
# Show uptime
local start_time=$(ps -p "$pid" -o lstart= 2>/dev/null)
if [[ -n "$start_time" ]]; then
log INFO "Started: $start_time"
fi
fi
# Show screen session info
echo ""
log INFO "Screen session details:"
sudo -u "$SERVER_USER" screen -list | grep "$SCREEN_SESSION_NAME" || log WARN "No screen session found"
else
log WARN "✗ Server is NOT RUNNING"
fi
echo ""
}
# Show server directory status
show_directory_status() {
log HEADER "=== Directory Status ==="
echo ""
if [[ -d "$SERVER_DIR" ]]; then
log INFO "✓ Server directory exists: $SERVER_DIR"
# Check for server executable
if [[ -f "$SERVER_DIR/$SERVER_EXECUTABLE" ]]; then
log INFO "✓ Server executable found"
if [[ -x "$SERVER_DIR/$SERVER_EXECUTABLE" ]]; then
log INFO "✓ Server executable is executable"
else
log WARN "✗ Server executable is not executable"
fi
else
log ERROR "✗ Server executable not found"
fi
# Check for world data
if [[ -d "$SERVER_DIR/worlds" ]]; then
local world_count=$(find "$SERVER_DIR/worlds" -maxdepth 1 -type d | wc -l)
log INFO "✓ Worlds directory found ($((world_count - 1)) worlds)"
else
log WARN "✗ Worlds directory not found"
fi
# Check for configuration files
for file in "${PRESERVE_FILES[@]}"; do
if [[ -f "$SERVER_DIR/$file" ]]; then
log INFO "✓ Configuration file: $file"
else
log WARN "✗ Configuration file missing: $file"
fi
done
# Show disk usage
local disk_usage=$(du -sh "$SERVER_DIR" 2>/dev/null | cut -f1)
log INFO "Disk usage: $disk_usage"
else
log ERROR "✗ Server directory not found: $SERVER_DIR"
log ERROR "Run update-server.sh to install the server"
fi
echo ""
}
# Show backup status
show_backup_status() {
log HEADER "=== Backup Status ==="
echo ""
if [[ -d "$BACKUP_DIR" ]]; then
log INFO "✓ Backup directory exists: $BACKUP_DIR"
local backup_count=$(find "$BACKUP_DIR" -name "minecraft-backup-*.tar.gz" -type f 2>/dev/null | wc -l)
log INFO "Available backups: $backup_count"
if [[ $backup_count -gt 0 ]]; then
echo ""
log INFO "Recent backups:"
find "$BACKUP_DIR" -name "minecraft-backup-*.tar.gz" -type f -printf "%T@ %s %p\n" 2>/dev/null | \
sort -nr | head -5 | while IFS=' ' read -r timestamp size path; do
local backup_name=$(basename "$path")
local size_mb=$((size / 1024 / 1024))
local backup_date=$(date -d "@$timestamp" '+%Y-%m-%d %H:%M:%S' 2>/dev/null || echo "unknown")
echo " $backup_name (${size_mb}MB) - $backup_date"
done
fi
# Show backup directory disk usage
local backup_disk_usage=$(du -sh "$BACKUP_DIR" 2>/dev/null | cut -f1)
log INFO "Backup directory disk usage: $backup_disk_usage"
else
log WARN "✗ Backup directory not found: $BACKUP_DIR"
fi
echo ""
}
# Show log information
show_log_status() {
log HEADER "=== Log Status ==="
echo ""
if [[ -f "$LOG_FILE" ]]; then
log INFO "✓ Log file exists: $LOG_FILE"
local log_size=$(du -sh "$LOG_FILE" 2>/dev/null | cut -f1)
log INFO "Log file size: $log_size"
local log_lines=$(wc -l < "$LOG_FILE" 2>/dev/null || echo "0")
log INFO "Log entries: $log_lines lines"
echo ""
log INFO "Recent log entries (last 10):"
tail -n 10 "$LOG_FILE" 2>/dev/null | while IFS= read -r line; do
echo " $line"
done
else
log WARN "✗ Log file not found: $LOG_FILE"
fi
echo ""
}
# Connect to server console
connect_to_console() {
if ! is_server_running; then
log ERROR "Server is not running"
return 1
fi
log INFO "Connecting to server console..."
log INFO "Press Ctrl+A, then D to detach from the console"
echo ""
# Connect to the screen session
sudo -u "$SERVER_USER" screen -r "$SCREEN_SESSION_NAME"
}
# Send command to server
send_command() {
local command="$1"
if ! is_server_running; then
log ERROR "Server is not running"
return 1
fi
log INFO "Sending command to server: $command"
sudo -u "$SERVER_USER" screen -S "$SCREEN_SESSION_NAME" -X stuff "$command"$'\r'
}
# Show usage information
show_usage() {
echo "Usage: $0 [COMMAND] [OPTIONS]"
echo ""
echo "Commands:"
echo " status Show complete server status (default)"
echo " connect Connect to server console"
echo " command <cmd> Send command to server"
echo " start Start the server"
echo " stop Stop the server gracefully"
echo " restart Restart the server"
echo " update Update the server"
echo ""
echo "Examples:"
echo " $0 Show server status"
echo " $0 connect Connect to server console"
echo " $0 command \"say Hello\" Send a message to players"
echo " $0 start Start the server"
echo " $0 stop Stop the server"
echo " $0 restart Restart the server"
}
# Main function
main() {
local command="${1:-status}"
case "$command" in
"status"|"")
show_server_status
show_directory_status
show_backup_status
show_log_status
;;
"connect"|"console")
connect_to_console
;;
"command"|"cmd")
if [[ $# -lt 2 ]]; then
log ERROR "Command argument required"
log ERROR "Usage: $0 command <server_command>"
exit 1
fi
send_command "$2"
;;
"start")
"$SCRIPT_DIR/start-server.sh"
;;
"stop")
"$SCRIPT_DIR/stop-server.sh"
;;
"restart")
"$SCRIPT_DIR/stop-server.sh"
sleep 5
"$SCRIPT_DIR/start-server.sh"
;;
"update")
"$SCRIPT_DIR/update-server.sh"
;;
"help"|"-h"|"--help")
show_usage
;;
*)
log ERROR "Unknown command: $command"
show_usage
exit 1
;;
esac
}
# Run main function
main "$@"