-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminecraft_service.sh
executable file
·187 lines (167 loc) · 4.97 KB
/
minecraft_service.sh
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
#!/bin/sh
# Exit on errors and undefined variables
set -eu
# Define the location of the config file
CONFIG_FILE="/etc/minecraft_config.sh"
# Source the configuration file
# shellcheck source=minecraft_config.sh
. "$CONFIG_FILE"
# Function to run a command as MINECRAFT_USER if the current user is not MINECRAFT_USER
run_as_minecraft_user() {
if [ "$(id -u -n)" = "$MINECRAFT_USER" ]; then
/bin/sh -c "$*"
else
su "$MINECRAFT_USER" -c "$*"
fi
}
minecraft_start() {
if [ ! -d "$MINECRAFT_DIR" ]; then
echo "Minecraft server directory $MINECRAFT_DIR does not exist."
return 1
fi
if ! session_running; then
echo "Starting Minecraft server..."
mkdir -p "$TMUX_SOCKET_DIR"
chown -R "$MINECRAFT_USER:$MINECRAFT_GROUP" "$TMUX_SOCKET_DIR"
run_as_minecraft_user "$TMUX_PATH -S $TMUX_SOCKET_PATH new-session -d -s $TMUX_SESSION -c $MINECRAFT_DIR \"$START_COMMAND\""
echo "Minecraft server started in detached tmux session '$TMUX_SESSION'."
pid=$(run_as_minecraft_user "$TMUX_PATH -S $TMUX_SOCKET_PATH list-panes -t $TMUX_SESSION -F '#{pane_pid}'")
if [ "$(echo "$pid" | wc -l)" -ne 1 ]; then
echo "Failed to determine server PID, multiple active tmux sessions."
return 1
fi
printf "%s" "$pid" >"$PID_PATH"
for user in $(getent group "$MINECRAFT_GROUP" | cut -d ':' -f 4 | tr ',' '\n'); do
if [ "$user" != "$MINECRAFT_USER" ] && [ -n "$user" ]; then
run_as_minecraft_user "$TMUX_PATH -S $TMUX_SOCKET_PATH server-access -a \"$user\""
fi
done
chown -R "$MINECRAFT_USER:$MINECRAFT_GROUP" "$TMUX_SOCKET_DIR"
chmod 660 "$TMUX_SOCKET_PATH"
chmod 770 "$TMUX_SOCKET_DIR"
else
echo "A tmux session named '$TMUX_SESSION' is already running."
fi
}
session_running() {
run_as_minecraft_user "$TMUX_PATH -S $TMUX_SOCKET_PATH has-session -t $TMUX_SESSION" 2>/dev/null
}
issue_cmd() {
command="$*"
run_as_minecraft_user "$TMUX_PATH -S $TMUX_SOCKET_PATH send-keys -t $TMUX_SESSION.0 \"$command\" C-m"
}
minecraft_stop() {
if ! session_running; then
echo "The server is already stopped."
if [ -f "$PID_PATH" ]; then
rm "$PID_PATH"
return $? # Return the status of the rm command
else
return 0 # Return 0 if the PID file does not exist
fi
fi
# Warn players with a 20-second countdown
echo "Warning players..."
for i in $(seq 20 -1 1); do
issue_cmd "say Shutting down in $i second(s)"
if [ $((i % 5)) -eq 0 ]; then
echo "$i seconds remaining..."
fi
sleep 1
done
# Issue the stop command
echo "Stopping server..."
if ! issue_cmd "stop"; then
echo "Failed to send stop command to server."
return 1
fi
# Wait for the server to stop
echo "Waiting for server to stop..."
wait=0
while session_running; do
sleep 1
wait=$((wait + 1))
if [ $wait -gt 60 ]; then
echo "Timed out waiting for server to stop."
return 1
fi
done
echo "Server stopped successfully."
if [ -f "$PID_PATH" ]; then
rm "$PID_PATH"
return $? # Return the status of the rm command
else
return 0 # Return 0 if the PID file does not exist
fi
}
minecraft_log() {
if [ -f "$MINECRAFT_DIR/logs/latest.log" ]; then
tail -f "$MINECRAFT_DIR/logs/latest.log"
else
echo "Log file does not exist."
fi
}
minecraft_attach() {
if session_running; then
run_as_minecraft_user "TERM=screen-256color $TMUX_PATH -S $TMUX_SOCKET_PATH attach-session -t $TMUX_SESSION.0" ||
run_as_minecraft_user "TERM=screen $TMUX_PATH -S $TMUX_SOCKET_PATH attach-session -t $TMUX_SESSION.0"
else
echo "No tmux session named '$TMUX_SESSION' is running."
fi
}
minecraft_cmd() {
if [ $# -eq 0 ]; then
echo "No command provided. Usage: $0 cmd '<command>'"
return 1
fi
command="$*"
if session_running; then
issue_cmd "$command"
echo "Command '$command' sent to Minecraft server."
else
echo "No tmux session named '$TMUX_SESSION' is running."
fi
}
minecraft_reload() {
if session_running; then
issue_cmd "reload"
echo "Reload command sent to Minecraft server."
else
echo "No tmux session named '$TMUX_SESSION' is running."
fi
}
minecraft_status() {
if session_running; then
echo "Minecraft server is running in tmux session '$TMUX_SESSION'."
else
echo "Minecraft server is not running."
fi
}
case "$1" in
start)
minecraft_start
;;
stop)
minecraft_stop
;;
log)
minecraft_log
;;
attach)
minecraft_attach
;;
cmd)
shift
minecraft_cmd "$@"
;;
reload)
minecraft_reload
;;
status)
minecraft_status
;;
*)
echo "Usage: $0 {start|stop|log|attach|cmd|reload|status}"
exit 2
;;
esac