forked from Ahtenus/minecraft-init
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminecraft
More file actions
executable file
·211 lines (199 loc) · 5.41 KB
/
minecraft
File metadata and controls
executable file
·211 lines (199 loc) · 5.41 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
#!/bin/bash
# /etc/init.d/minecraft
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Minecraft server
# Description: Init script for minecraft/bukkit server, with rolling logs and use of ramdisk for less lag.
### END INIT INFO
### Based on http://www.minecraftwiki.net/wiki/Server_startup_script
# Name of server.jar file
SERVICE='craftbukkit-0.0.1-SNAPSHOT.jar'
# User that should run the server
USERNAME="v"
# Path to minecraft directory
MCPATH='/home/v/mine'
CPU_COUNT=2
# Change the values of -Xmx###M and -Xms###M to how mutch ram you want to give to the
# server, remember to make room for the ramdisk
INVOCATION="java -Xmx760M -Xms760M -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar $SERVICE nogui"
# Where the world backups should go
BACKUPPATH='/home/v/minebak'
# Where the logs are copied when running log-roll
LOGPATH="${MCPATH}/logs"
# Where the whole minecraft directory is copied when whole-backup is runned
WHOLEBACKUP='/home/v/serverbak'
# Where the world is located on the disk
WORLDSTORAGE='/home/v/mine/diskworld'
#Path to the the mounted ramdisk default in ubuntu: /dev/shm
RAMDISK='/dev/shm'
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
datepath() {
# datepath path filending-to-check returned-filending
if [ -e $1`date +%x`$2 ]
then
echo $1`date +%FT%T`$3
else
echo $1`date +%F`$3
fi
}
mc_start() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "Tried to start but $SERVICE was already running!"
else
echo "$SERVICE was not running... starting."
# rsync -tr $MCPATH/world-disc /dev/shm/
cd $MCPATH
as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION"
sleep 7
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is now running."
else
echo "Could not start $SERVICE."
fi
fi
}
mc_saveoff() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... suspending saves"
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
else
echo "$SERVICE was not running. Not suspending saves."
fi
}
mc_saveon() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... re-enabling saves"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"
else
echo "$SERVICE was not running. Not resuming saves."
fi
}
mc_stop() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... stopping."
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
sleep 10
as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
sleep 7
else
echo "$SERVICE was not running."
fi
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE could not be shut down... still running."
else
echo "$SERVICE is shut down."
fi
}
log_roll() {
path=`datepath $MCPATH/logs/server_ .log.gz .log`
as_user "mv $MCPATH/server.log $path && gzip $path"
}
mc_whole_backup() {
path=`datepath $WHOLEBACKUP/mine_`
as_user "cp -r $MCPATH $path"
}
mc_world_backup() {
echo "Backing up minecraft world"
path=`datepath $BACKUPPATH/world_ .tar.bz2 .tar.bz2`
as_user "tar -hcjf $path $MCPATH/world"
}
to_ram() {
if [ -L $MCPATH/world ]
then
as_user "rsync -rt $WORLDSTORAGE/ $RAMDISK/world"
echo "World copied to ram"
fi
}
to_disk() {
as_user "rsync -rt $MCPATH/world/ $WORLDSTORAGE"
echo "World copied to disk"
}
case "$1" in
start)
# Starts the server
to_ram
mc_start
;;
stop)
# Stops the server
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER SHUTTING DOWN!\"\015'"
mc_stop
to_disk
;;
restart)
# Restarts the server
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER REBOOT IN 10 SECONDS.\"\015'"
mc_stop
mc_start
;;
backup)
# Backups world
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say Backing up world.\"\015'"
mc_saveoff
mc_world_backup
mc_saveon
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say Backup complete.\"\015'"
;;
whole-backup)
# Backups everything
mc_whole_backup
;;
to-disk)
# Writes from the ramdisk to disk, in case the server crashes.
# Using ramdisk speeds things up alot, especially if you allow
# teleportation on the server.
mc_saveoff
to_disk
mc_saveon
;;
connected)
# Lists connected users
as_user "screen -p 0 -S minecraft -X eval 'stuff list\015'"
sleep 3s
tac $MCPATH/server.log | grep -m 1 "Connected"
;;
log-roll)
# Moves and Gzips the logfile, a big log file slows down the
# server ALOT (what was notch thinking?)
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say ROUTINE REBOOT IN 10 SECONDS.\"\015'"
mc_stop
log_roll
mc_start
;;
status)
# Shows server status
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
*)
echo "Usage: /etc/init.d/minecraft {start|stop|backup|whole-backup|log-roll|to-disk|connected|status|restart}"
exit 1
;;
esac
exit 0