Thursday, February 7, 2013

Minecraft: Running a Server on FreeBSD

I'd previously posted about running Minecraft on FreeBSD, but that was a year ago, and I didn't give many details about the server. (I haven't even tried to run the client on FreeBSD in a long time, both because I just leave my desktop to the memory-hogging server process and because I haven't been playing recently.) So here's my server setup.

Note: I'm running a custom build of FreeBSD 9.0 (I haven't gotten around to upgrading to 9.1 yet). Mostly I just removed drivers for hardware I don't have. Also, I created a separate user account for running the server. As I don't run around as root most of the time, I set up sudo to allow my regular user login the ability to run commands as the minecraft user. (I called mine "mc".)

The Server JAR File: I just use the plain jar from

The Java Port: There are a bunch of JVM ports in the FreeBSD Ports tree. I tried them all. The one that has provided the most stability is openjdk7.

CraftBukkit: I am running a Craftbukkit server with about 10 plugins.

Screen: GNU Screen is very, very helpful here. It will let you run on the console and be able to connect to the running session from a remote login to see what's going on, without interrupting the minecraft server process. (In the FreeBSD ports tree, it's sysutils/screen. RTFM if you've never used it before; it's a little complicated at first, but becomes second nature with use.)

I start the initial screen session as follows (I name the session "stuff" for the backup script, below): screen -S stuff. Then I name the first shell (window 0) by pressing Ctrl-a A (control+a keys together, release, then capital 'A') so it will prompt me to rename that window. I name it "mc", again for the backup script.

The Startup Script: (I named it, but it doesn't matter.)

umask 0002
export JAVA_HOME=/usr/local/openjdk7
java -Xincgc -Xms1G -Xmx2816M -jar craftbukkit-1.4.7-R1.0.jar

Run it in the directory in which you have the server jar file and the craftbukkit jar file. Note that if you're not using craftbukkit, you can replace the java command line with the one on the download page. Note also you need to update the craftbukkit jar file name as you upgrade, or you could name them all craftbukkit.jar and leave the script alone. (The first method takes a little more effort, but makes it easier to see what you're running.)

If you're running the server as another user, execute as sudo -u $MINECRAFT_USER ./

The Backup Script: (I got the basics from somewhere that I've forgotten, and altered it to my needs.)


export PATH=/bin:/usr/bin:/usr/local/bin

echo "Forcing save-all"
screen -x -p mc -X stuff `printf "save-off\r"`
sleep 5
screen -x -p mc -X stuff `printf "save-all\r"`
sleep 15
tar czvf /home/mc/backups/server.`date +%Y%m%d%H%M%S`.tgz /home/mc/server
screen -x -p mc -X stuff `printf "save-on\r"`
echo "Backup complete!"

You can then add a crontab to run it however often you want, but also make sure you add one to clean out old backup files, because they can eat up space fast. Make sure to change the directory names in the tar command, and make sure the target directory exists. Note that the screen ... 'printf ...' commands should actually get picked up by the server process. They pause disk writes so that you will get a clean backup.


Anonymous said...

Very cool info. What are the hardware specs for your server?

Karen said...

It's definitely not a high end machine. 2 x Phenom II X2 521 3.50GHz, 5.7GB, 2 x 1TB SATA (7200RPM)

I don't run X on it, except for brief, occasional instances. I do other random stuff on it, but nothing prolonged or intensive. The load is never high, and it doesn't tend to swap.

Network latency is the real bottleneck for players.