-
Notifications
You must be signed in to change notification settings - Fork 14
/
service.sh
executable file
·119 lines (100 loc) · 2.16 KB
/
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
#!/bin/bash
# Minecraft service that starts the minecraft server in a tmux session
MC_HOME="/var/minecraft"
MC_PID_FILE="$MC_HOME/minecraft-server.pid"
MC_START_CMD="java -Xmx8G -Xms256M -jar spigot.jar"
TMUX_SOCKET="minecraft"
TMUX_SESSION="minecraft"
is_server_running() {
tmux -L $TMUX_SOCKET has-session -t $TMUX_SESSION > /dev/null 2>&1
return $?
}
mc_command() {
cmd="$1"
tmux -L $TMUX_SOCKET send-keys -t $TMUX_SESSION.0 "$cmd" ENTER
return $?
}
start_server() {
if is_server_running; then
echo "Server already running"
return 1
fi
echo "Starting minecraft server in tmux session"
tmux -L $TMUX_SOCKET new-session -c $MC_HOME -s $TMUX_SESSION -d "$MC_START_CMD"
pid=$(tmux -L $TMUX_SOCKET list-sessions -F '#{pane_pid}')
if [ "$(echo $pid | wc -l)" -ne 1 ]; then
echo "Could not determine PID, multiple active sessions"
return 1
fi
echo -n $pid > "$MC_PID_FILE"
return $?
}
stop_server() {
if ! is_server_running; then
echo "Server is not running!"
return 1
fi
# Warn players
echo "Warning players"
mc_command "title @a times 3 14 3"
for i in {10..1}; do
mc_command "title @a subtitle {\"text\":\"in $i seconds\",\"color\":\"gray\"}"
mc_command "title @a title {\"text\":\"Shutting down\",\"color\":\"dark_red\"}"
sleep 1
done
# Issue shutdown
echo "Kicking players"
mc_command "kickall"
echo "Stopping server"
mc_command "stop"
if [ $? -ne 0 ]; then
echo "Failed to send stop command to server"
return 1
fi
# Wait for server to stop
wait=0
while is_server_running; do
sleep 1
wait=$((wait+1))
if [ $wait -gt 60 ]; then
echo "Could not stop server, timeout"
return 1
fi
done
rm -f "$MC_PID_FILE"
return 0
}
reload_server() {
tmux -L $TMUX_SOCKET send-keys -t $TMUX_SESSION.0 "reload" ENTER
return $?
}
attach_session() {
if ! is_server_running; then
echo "Cannot attach to server session, server not running"
return 1
fi
tmux -L $TMUX_SOCKET attach-session -t $TMUX_SESSION
return 0
}
case "$1" in
start)
start_server
exit $?
;;
stop)
stop_server
exit $?
;;
reload)
reload_server
exit $?
;;
attach)
attach_session
exit $?
;;
*)
echo "Usage: ${0} {start|stop|reload|attach}"
exit 2
;;
esac