This repository has been archived by the owner on May 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server
291 lines (262 loc) · 6.98 KB
/
server
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/bash
#
# MCManager version 2.0
# Minecraft bukkit server manager
#
# By Jerom van der Sar - Later worked and developed by Ryan Wild
#################################################################
# Load the config
source config.cfg
function infomsg() {
cecho "&6[INFO]&r $1"
}
function warningmsg() {
cecho "&3[WARN]&r $1"
}
function errormsg() {
cecho "&1[SEVERE]&r $1"
}
function okmsg() {
cecho "&2[FINE]&r $1"
}
ME=`whoami`
function as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
function mc_start() {
if [ -e "$MCPATH/restart_in_progress" ]
then
warningmsg "Server restart was aborted improperly!"
rm "$MCPATH/restart_in_progress"
fi
cd $MCPATH
infomsg "Version `java -jar $MCPATH/$SERVICE --version`"
as_user "cd $MCPATH && screen -h $HISTORY -dmS minecraft $INVOCATION"
sleep $STARTTIME
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
okmsg "Server is &2running&r."
else
errormsg "Could not start Minecraft server!"
fi
}
function mc_saveoff() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
infomsg "Server is &2running&r... Suspending saves"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say **SERVER IS BACKING UP**\"\015'"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
sync
sleep 10
fi
}
function mc_saveon() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
infomsg "Server is &2running&r... Re-enabling saves"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say **SERVER BACKUP COMPLETE**\"\015'"
fi
}
function mc_stop() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say **SERVER IS ABOUT TO SHUT DOWN**\"\015'"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
sleep 9
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say **SERVER IS SHUTTING DOWN**\"\015'"
sleep 1
as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
sleep $STOPTIME
else
warningmsg "Server is not running."
exit
fi
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
errormsg "Server could not be stopped."
else
okmsg "Server &1stopped&r."
fi
}
function mc_restart() {
mc_start()
mc_stop()
}
function mc_update() {
case "$1" in
spigot)
DOWNLOAD_URL=$SPIGOTURL
;;
*)
warningmsg "Please use 'spigot'"
exit
;;
esac
##mc_backup
infomsg "Download url: $DOWNLOAD_URL"
infomsg "Dowloading Update..."
as_user "cd $MCPATH && wget -q -O $MCPATH/spigot.jar.update $DOWNLOAD_URL"
okmsg "Download complete!"
if [ -f $MCPATH/spigot.jar.update ]
then
if `diff $MCPATH/$SERVICE $MCPATH/spigot.jar.update >/dev/null`
then
warningmsg "The latest version of $SERVICE is already installed."
#rm $MCPATH/craftbukkit.jar.update
#exit
fi
running="false"
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
infomsg "Server is &2running&r... Stopping server for CraftBukkit update"
running="true"
mc_stop
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
exit
fi
fi
infomsg "Updating CraftBukkit..."
as_user "mv $MCPATH/craftbukkit.jar.update $MCPATH/$SERVICE"
if [[ $running == "true" ]]
then
infomsg "Starting server..."
mc_start
fi
okmsg "Server successfully updated."
else
errormsg "Spigot update could not be downloaded."
fi
}
function mc_backup() {
if [ -e "$MCPATH/restart_in_progress" ]
then
errormsg "A restart is currently running."
errormsg "Aborting..."
exit
fi
mc_saveoff
#### START BACKUP
NOW=`date "+%Y-%m-%d_%Hh%M"`
BACKUP_FILE="$BACKUPPATH/${WORLD}_${NOW}.tar"
infomsg "Backing up minecraft world..."
# as_user "cd $MCPATH && cp -r $WORLD $BACKUPPATH/${WORLD}_`date "+%Y.%m.%d_%H.%M"`"
as_user "tar -C \"$MCPATH/worlds/world\" -cf \"$BACKUP_FILE\" $WORLD"
infomsg "Backing up $SERVICE..."
as_user "tar -C \"$MCPATH\" -rf \"$BACKUP_FILE\" $SERVICE"
# as_user "cp \"$MCPATH/$SERVICE\" \"$BACKUPPATH/minecraft_server_${NOW}.jar\""
#### END BACKUP
mc_saveon
infomsg "Compressing backup..."
as_user "gzip -f \"$BACKUP_FILE\""
okmsg "Backup Complete."
}
function mc_command() {
command="$1";
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
pre_log_len=`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`
as_user "screen -p 0 -S minecraft -X eval 'stuff \"$command\"\015'"
sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
# print output
tail -n $[`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`-$pre_log_len] "$MCPATH/logs/latest.log"
fi
}
function mc_say() {
message="$1";
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say $message\"\015'"
infomsg "&5[Server] $1"
fi
}
# Start-Stop here
case "$1" in
start)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
infomsg "The server is already running!"
else
infomsg "Starting Minecraft server..."
mc_start
fi
;;
stop)
if [ -e "$MCPATH/restart_in_progress" ]
then
errormsg "A restart is already running."
errormsg "Aborting..."
exit
fi
infomsg "Stopping server..."
mc_stop
;;
restart)
mc_restart
;;
update)
if [ $# -eq 2 ]; then
#mc_backup
mc_update "$2"
else
warningmsg "Must specify branch ('glowstone')"
fi
;;
backup)
mc_backup
;;
status)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
infomsg "Server is &2running&r."
else
infomsg "Server is &1stopped&r."
fi
infomsg "Version: `java -jar $MCPATH/$SERVICE --version`"
;;
cmd)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
if [ $# -gt 1 ]; then
shift
mc_command "$*"
else
warningmsg "Must specify server command (try 'help')"
fi
else
warningmsg "Server is not running!"
fi
;;
say)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
if [ $# -gt 1 ]; then
shift
mc_say "$*"
else
warningmsg "Must specify message"
fi
else
warningmsg "Server is not running!"
fi
;;
*)
cecho "&3Usage: $0"
cecho " &6start &r- &6Starts the server"
cecho " &6stop &r- &6Stops the server"
cecho " &6update <spigot> &r- &6Updates the server to a branch"
cecho " &6backup &r- &6Makes a backup of the world"
cecho " &6status &r- &6Shows server information"
cecho " &6restart &r- &6Restarts the server"
cecho " &6say (message) &r- &6Broadcasts a message to the server"
cecho " &6cmd (command) &r- &6Excecutes a command"
exit 1
;;
esac
exit 0