forked from TapeWerm/MCscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc_stop.sh
executable file
·80 lines (72 loc) · 1.61 KB
/
mc_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
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
syntax='Usage: mc_stop.sh [OPTION]... SERVICE'
server_do() {
echo "$*" > "/run/$service"
}
countdown() {
warning="Server stopping in $*"
server_do say "$warning"
echo "$warning"
}
args=$(getopt -l help,seconds: -o hs: -- "$@")
eval set -- "$args"
while [ "$1" != -- ]; do
case $1 in
--help|-h)
echo "$syntax"
echo Warn Minecraft Java Edition or Bedrock Edition server running in service 10 seconds before stopping.
echo
echo Mandatory arguments to long options are mandatory for short options too.
echo '-s, --seconds=SECONDS seconds before stopping. must be between 0 and 60. defaults to 10'
echo
echo Best ran by systemd before shutdown.
exit
;;
--seconds|-s)
seconds=$2
if [[ ! "$seconds" =~ ^-?[0-9]+$ ]]; then
>&2 echo SECONDS must be an integer
exit 1
fi
if [ "$seconds" -lt 0 ] || [ "$seconds" -gt 60 ]; then
>&2 echo SECONDS must be between 0 and 60
exit 1
fi
shift 2
;;
esac
done
shift
if [ "$#" -lt 1 ]; then
>&2 echo Not enough arguments
>&2 echo "$syntax"
exit 1
elif [ "$#" -gt 1 ]; then
>&2 echo Too much arguments
>&2 echo "$syntax"
exit 1
fi
service=$1
if [ -z "$MAINPID" ]; then
MAINPID=$(systemctl show "$service" -p MainPID --value)
fi
if [ "$MAINPID" = 0 ]; then
echo "Service $service already stopped"
exit
fi
if [ -z "$seconds" ]; then
seconds=10
fi
if [ "$seconds" -gt 3 ]; then
countdown "$seconds seconds"
sleep $((seconds - 3))
fi
for x in {3..1}; do
if [ "$seconds" -gt $((x - 1)) ]; then
countdown "$x seconds"
sleep 1
fi
done
server_do stop
# Follow /dev/null until $MAINPID dies
tail -f --pid "$MAINPID" /dev/null