Linux Minecraft Server Script (multi-server, multi-world)

Introduction

We've been running Minecraft servers on our Linux machine for about a year now and we started off using the startup script from the minecraft wiki here.

Running Multiple Servers

This is a simple BASH script which uses the "screen" utility to provide basic startup & shutdown along with more advanced features such as backup and update. Generally it works great, but we found it has a few restrictions, one being it can only handle one service per machine, and other being it's inability to backup multiworld servers.

So here's the updated version that we're now using.

Installing the Script

I'm going to assume you've successfully installed the minecraft server on your linux machine and you have the 'screen' utility installed. (If not then follow these instructions)

1 - Download the startup script and copy it to your /etc/init.d directory.

2 - Edit the script and update the #Settings section so that it matches the location of your files and the jar name. The world should be the name of the new world you wish to create, or one that already exists. Here's mine as an example..

#Settings
SERVICE='minecraft_server.jar'
SERVICENAME='hexxit-server'
OPTIONS='nogui'
USERNAME='root'
WORLD='YOLO_HEXXIT'
OTHERWORLDS='creative hungergames spawn griefcity'
MCPATH='/opt/Hexxit'
BACKUPPATH='/backup/minecraft'
MAXHEAP=2048
MINHEAP=2048
HISTORY=1024
CPU_COUNT=1

(nb. Make sure the servicename doesn't match the script name - don't just use minecraft or it will think the server's always running!)

3 - If you are using a multiworld patch then you can add the names of them into the OtherWorlds variable (for backup purposes, it's not required to make it work).

4 - Save it and ensure the user that will run it has the execute flag set.
chmod a+x /etc/init.d/minecraft

5 - Start your server using the follow command..
/etc/init.d/minecraft start

If you want the service to start automatically you need to add the script to the linux startup:-

On Debian based distributions (such as Ubuntu) this will be..
update-rc.d minecraft defaults

For CentOS or RedHat this will be..
chkconfig --add minecraft


Scheduling Backups

It's quite easy to set up a daily backup just in case you get greifing problems from some of your players.

1 - Create a backup directory on your server.
mkdir /backup
mkdir /backup/minecraft

2 - Edit your startup script and add this to the BackupPath in your #Settings section (as per my example above).

3 - Save your script.

4 - Test this by entering the following command..
/etc/init.d/minecraft backup

5 - Check the minecraft log entries, it should now show..
2014-07-30 18:23:56 [INFO] [Server] SERVER BACKUP STARTING. Server going readonly...
2014-07-30 18:23:56 [INFO] Turned off world auto-saving
2014-07-30 18:23:56 [INFO] Saving...
2014-07-30 18:23:56 [INFO] Saved the world
2014-07-30 18:24:10 [INFO] Turned on world auto-saving
2014-07-30 18:24:10 [INFO] [Server] SERVER BACKUP ENDED. Server going read-write...

6 - Check your backup directory and a tar file will have been created. (This will contain your world data and the server jar file.)
ls /backup/minecraft
YOLO_HEXXIT_2014-07-30_18h24.tar.gz

7 - Type the following to edit your crontab..
crontab -e

8 - Add the following line..
10 23 * * * /etc/init.d/minecraft backup

(nb. this will run the backup at 23:10 every day)

9 - Save and Exit.



View the Server Log

To view the minecraft server console if you just have one service installed then you just need to type.. 
screen -r

If you have more than one installed then this will result in the following..
There are several suitable screens on:
3770.minecraft-server (25/07/14 21:31:16) (Detached)
3740.hexxit-server (25/07/14 21:31:03) (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

Ignore the instructions on screen, all you need to do is use the -S flag to select the session you want..
screen -r -S minecraft-server

Once you've finish press 'ctrl+a+d' to exit.

Add Another Server

Adding another server is pretty trivial with the new script.

1 - Make a copy of your original server directory (or install an alternative server type such as CraftBukkit, FeedTheBeast or Hexxit)

2 - If you've copied from your original server then you might want to delete all your config and worlds data and start from scratch. At very least you want to alter the server-port setting.

3 - Make a copy of the minecraft script in your /etc/init.d directory and give the file a new name.
cp /init.d/minecraft /init.d/craftbukkit

4 - Edit the script and alter the settings page to match the server service, ensuring your service name is different to your original version. (this is the identifying name that stops the two services from colliding when starting and stopping) Here's another example..

#Settings
SERVICE='craftbukkit-1.6.2-R0.1.jar'
SERVICENAME='bukkit-server'
OPTIONS='nogui'
USERNAME='root'
WORLD='Pinga'
OTHERWORLDS=''
MCPATH='/opt/bukkit'
BACKUPPATH='/backup/bukkit'
MAXHEAP=2048
MINHEAP=2048
HISTORY=1024
CPU_COUNT=1

5 - Save and exit the script.

6 - Make another backup directory
mkdir /backup/bukkit

7 - Add another backup entry into your crontab.

8 - Stop any other Minecraft servers that might already be running (just in case) and run the new script to check it works.
/etc/init.d/craftbukkit start

9 - Check you can join it using the Minecraft game and then stop it again..
/etc/init.d/craftbukkit stop

10 - If you haven't already done so, remember to enter a different server-port number in your server.properties file. (I used 25566 instead of the normal 25565)

That's pretty much it,.. you should now be able to start and stop both services without them impacting each other. You can carry on adding as many services as your Linux machine can handle, but don't add too many as it's greedy for resources once you get more than a few users connected.

No comments:

Post a Comment