-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmc.sh
More file actions
executable file
·239 lines (196 loc) · 5.89 KB
/
mc.sh
File metadata and controls
executable file
·239 lines (196 loc) · 5.89 KB
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
#!/bin/sh
#-
#
# Copyright (c) 2013 Jonathan Price <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# mc.sh
#
# Server managing script a Minecraft server using tmux. Includes
# commands to start, stop and restart the server, as well as connecting to
# the console
#
# Comments, suggestions, bug reports please to:
# Jonathan Price <[email protected]>
#
# NOTE: REQUIRES TMUX TO BE INSTALLED
# Name of tmux session
NAME="mc"
# Max memory (-Xmx)
MEM="800M"
# Location of minecraft jar file
DIR="/home/minecraft/minecraft/"
# Name of minecraft jar file
BIN="minecraft.jar"
# STARTUP PARAMTERS
# DO NOT EDIT THIS SECTION
# Shows the correct arguments to use this script
USAGE="Usage: $0 {start|stop|restart|stopnow|restartnow|console|status}"
# Determines whether there is a tmux server running
TMUXPID=$(pgrep -u "$(whoami)" -f "tmux" )
# Stores the directory the user was in when they ran the script
PWD=$(pwd)
# Is "stopping" or "restarting" based on command given
STATUS=""
# FUNCTIONS
# DO NOT EDIT THIS SECTION
# Sets the Java startup arguments for the server, with personalised maximum heap size
setJavaParams() {
JAVAPARAM="-Xmx${MEM} -jar"
}
# Starts the server, performing the following pre-startup checks:
# is tmux installed
# Does a tmux session with the same name already exist
# does the game folder $DIR exist
doStart() {
if [ ! "$(command -v tmux)" ]; then
echo "ERROR: tmux could not be found, is it installed?"
exit 1
fi
if [ "$TMUXPID" ]; then
if [ -n "$(tmux list-sessions | grep $NAME)" ]; then
echo "ERROR: A tmux session with the name $NAME already exists"
exit 1
fi
fi
if [ ! -d $DIR ]; then
echo "ERROR: Could not find $DIR. Is the path correct?"
exit 1
fi
echo "Starting $1"
setJavaParams
cd $DIR
tmux new-session -d -s $NAME "java $JAVAPARAM $BIN"
tmux attach-session -t $NAME
cd $PWD
}
# Stops the server, performing the following checks first
# Checks if tmux is running
# Checks if a tmux session with the correct name exists
doStop() {
if [ ! "$TMUXPID" ]; then
echo "ERROR: tmux is not running"
exit 1
fi
if [ ! -n "$(tmux list-sessions | grep $NAME)" ]; then
echo "ERROR: A tmux session with the name $NAME couldn't be found"
exit 1
fi
echo "Giving 10 second countdown warning."
tmux send-keys -t $NAME "save-all" ENTER
for i in 10 9 8 7 6 5 4 3 2 1; do
printf "%s " "$i"
tmux send-keys -t $NAME "say The server is $STATUS in $i seconds." ENTER
sleep 1
done
tmux send-keys -t $NAME "stop" ENTER
tmux attach-session -t $NAME
while [ -n "$(tmux list-session | grep $NAME)" ]; do
sleep 1
done
}
# Stops the server without warning, performing the following checks first
# Checks if tmux is running
# Checks if a tmux session with the correct name exists
doStopNow() {
if [ ! "$TMUXPID" ]; then
echo "ERROR: tmux is not running"
exit 1
fi
if [ ! -n "$(tmux list-sessions | grep $NAME)" ]; then
echo "ERROR: A tmux session with the name $NAME couldn't be found"
exit 1
fi
tmux send-keys -t $NAME "save-all" ENTER
sleep 1
tmux send-keys -t $NAME "stop" ENTER
tmux attach-session -t $NAME
while [ -n "$(tmux list-session | grep $NAME)" ]; do
sleep 1
done
}
# Restarts the server. For checks, see stop and start functions.
doRestart() {
doStop
doStart
}
# Restarts the server without warning. For checks, see stop and start functions.
doRestartNow() {
doStopNow
doStart
}
# Attaches the user's shell into the tmux session.
doConsole() {
tmux attach-session -t $NAME
}
# Returns some basic information about the server
doStatus() {
echo "Game: Minecraft | tmux session: $NAME"
if [ -n "$(tmux list-sessions | grep $NAME)" ]; then
echo "Status: running"
else
echo "Status: not running"
fi
}
# COMMAND INTERPRETER
# DO NOT EDIT THIS SECTION
if [ $# -ne 1 ]; then
echo "Incorrect number of arguments specified"
echo "$USAGE"
exit 2
elif [ -z "$1" ]; then
echo "Missing {start|stop|restart|stopnow|restartnow|console|status}"
echo "$USAGE"
exit 2
fi
case $1 in
start)
doStart
;;
stop)
STATUS="stopping"
doStop
;;
restart)
STATUS="restarting"
doRestart
;;
stopnow)
doStopNow
;;
restartnow)
doRestartNow
;;
console)
doConsole
;;
status)
doStatus
;;
*)
echo "$2 does not exist."
echo "$USAGE"
exit 2
;;
esac