forked from TheRemote/MinecraftBedrockServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.sh
67 lines (61 loc) · 1.92 KB
/
stop.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
#!/bin/bash
# James Chambers - https://jamesachambers.com/
# Minecraft Server stop script - primarily called by minecraft service but can be ran manually
# Check if server is running
if ! screen -list | grep -q "\.servername"; then
echo "Server is not currently running!"
exit 1
fi
# Get an optional custom countdown time (in minutes)
CountdownTime=0
while getopts ":t" opt; do
case $opt in
t)
case $string in
''|*[!0-9]*)
echo "Countdown time must be a whole number in minutes."
exit 1
;;
*)
CountdownTime=$OPTARG >&2
;;
esac
;;
\?)
echo "Invalid option: -$OPTARG; countdown time must be a whole number in minutes." >&2
;;
esac
done
# Stop the server
while [ $CountdownTime -gt 0 ]; do
if [ $CountdownTime -eq 1 ]; then
screen -Rd servername -X stuff "say Stopping server in 60 seconds...$(printf '\r')"
sleep 30;
screen -Rd servername -X stuff "say Stopping server in 30 seconds...$(printf '\r')"
sleep 20;
screen -Rd servername -X stuff "say Stopping server in 10 seconds...$(printf '\r')"
sleep 10;
else
screen -Rd servername -X stuff "say Stopping server in $CountdownTime minutes...$(printf '\r')"
sleep 60;
fi
echo "Waiting for $CountdownTime more minutes ..."
done
echo "Stopping Minecraft server ..."
screen -Rd servername -X stuff "say Stopping server (stop.sh called)...$(printf '\r')"
screen -Rd servername -X stuff "stop$(printf '\r')"
# Wait up to 20 seconds for server to close
StopChecks=0
while [ $StopChecks -lt 20 ]; do
if ! screen -list | grep -q "\.servername"; then
break
fi
sleep 1;
StopChecks=$((StopChecks+1))
done
# Force quit if server is still open
if screen -list | grep -q "\.servername"; then
echo "Minecraft server still hasn't stopped after 20 seconds, closing screen manually"
screen -S servername -X quit
fi
echo "Minecraft server servername stopped."