This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcctl.sh
executable file
·207 lines (191 loc) · 5.08 KB
/
mcctl.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
################ SETUP ################
exec_path=$(echo $PWD)
source "$exec_path"/mcctl.conf
################ SETUP ################
cd $exec_path
LOG () {
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
case "$1" in
info) # INFO
printf "\n$TIMESTAMP [INFO] ["$log_exec"] $2" >> $LOGFILE
printf "\n[INFO] $2"
;;
warn) # WARN
printf "\n$TIMESTAMP [WARN] ["$log_exec"] $2" >> $LOGFILE
printf "\n[WARN] $2"
;;
error) # ERROR
printf "\n$TIMESTAMP [ERROR] ["$log_exec"] $2" >> $LOGFILE
printf "\n[ERROR] $2"
;;
*) # Unknown
printf "\n$TIMESTAMP [?] ["$log_exec"] $2" >> $LOGFILE
printf "\n[?] $2"
LOG "error" "log -$1- not found! Please report to the script maintainer!"
;;
esac
printf "\n"
}
console() { # Passes commands to the console.
if screen -list | grep -q "$server_session"; then
LOG "info" 'Sending ''"'"$*"'"'' to console.'
screen -S "$server_session" -X stuff ''"$*\n"''
else
ERROR 2
fi
}
console-check() {
if screen -list | grep -q "$server_session"; then
shift 1
if [[ -n "$1" ]]; then console "$@"; else
LOG "info" "User $(whoami) opened the server console."
screen -r $screen_session
fi
else
ERROR 2
fi
}
start() {
if ! screen -list | grep -q "$server_session"; then
screen -AmdS $server_session $start_cmd
sleep 4s
if ! screen -list | grep -q "$server_watch_session"; then watchdog start; fi
else
ERROR 1
fi
exit 0
}
stop() {
if screen -list | grep -q "$server_session"; then
watchdog stop
all_timeout=$(expr "$1" + "10")
console 'save-all' # Save worlds.
sleep "1s"
console 'title @a actionbar {"text":"Server shutdown in '$all_timeout's!","color":"dark_red"}'
sleep "0.2s"
sleep ""$1"s"
for i in {10..1}
do
console 'title @a actionbar {"text":"Server shutdown in '$i's","color":"gold"}'
sleep 1s
done
sleep 0.5s
console 'title @a actionbar {"text":"Shutdown NOW!","color":"dark_red"}'
console 'stop'
else
ERROR 2
fi
}
restart() {
if screen -list | grep -q "$server_session"; then
watchdog stop
all_timeout=$(expr "$1" + "10")
console 'save-all' # Save worlds.
sleep "1s"
console 'title @a actionbar {"text":"Server Restart in '$all_timeout's!","color":"dark_red"}'
sleep "0.2s"
sleep ""$1"s"
for i in {10..1}
do
console 'title @a actionbar {"text":"Server Restart in '$i's","color":"gold"}'
sleep 1s
done
sleep 0.5s
console 'title @a actionbar {"text":"Restart NOW!","color":"dark_red"}'
sleep 0.2s
console 'stop'
sleep 4s
watchdog start
else
ERROR 2
fi
}
killp() {
if screen -list | grep -q "$server_session"; then
watchdog stop
screen -X -S $server_session kill
else
ERROR 2
fi
}
# Watchdog script handler.
watchdog() {
case "$1" in
start)
if ! screen -list | grep -q "$server_watch_session"; then
LOG "info" "Starting Watchdog.."
screen -AmdS "$server_watch_session" "./$watch_script"
else
ERROR 3
fi
;;
stop)
if screen -list | grep -q "$server_watch_session"; then
LOG "info" "Stopping Watchdog.."
screen -X -S "$server_watch_session" stuff "^C"
else
ERROR 4
fi
;;
kill)
if screen -list | grep -q "$server_watch_session"; then
LOG "warn" "Killing Watchdog.."
screen -X -S "$server_watch_session" kill
else
ERROR 4
fi
;;
*)
echo "Usage: ${0} watchdog {start|stop|ḱill}"
esac
}
ERROR() {
printf "\n"
case "$@" in
1) # Error code 1
LOG "warn" 'Screen session('"$server_session"') allready started!'
;;
2) # Error code 2
LOG "warn" 'Screen session('"$server_session"') is not running!'
;;
3) # Error code 2
LOG "warn" 'Screen session('"$server_watch_session"') allready started!'
;;
4) # Error code 2
LOG "warn" 'Screen session('"$server_watch_session"') is not running!'
;;
*) # Unknown error code
LOG "error" 'Unknown error '"$@"'!'
;;
esac
printf "\n"
}
################################ MAIN ################################
log_exec=$(echo $1)
case $1 in
console|c|co|con|cmd)
log_exec="console"
console-check "$@"
;;
start)
start # Starts the server.
;;
stop)
stop "$2" # Stops the Server and the Watchdog!
;;
restart)
restart "$2" # Restart the Server.
;;
kill)
killp # Kills the Server & Watchdog
;;
watchdog|wdog)
log_exec="watchdog"
watchdog "$2"
;;
*)
echo "Usage: ${0} {start|stop <count+10s>|restart <count+10s>|kill|watchdog|console|console 'command'}"
;;
esac
exit 0