Minecraft with Java and Tmux on FreeBSD

Standard

A virtual world cannot be complete without a virtual reality environment like Minecraft.  In this article, I quickly go through the steps setting a Minecraft server with FreeBSD.   Specifically, I will be installing a Spigot Minecraft Server.  I like it very much because it comes with lots of performance optimisations.  I assume you prepared your environment with my customisation script or something similar.  The whole process will take less than one hour if you have a good internet connection.

Step 1: Installing Java, Tmux, Bash, and Friends

As usual, installing packages in FreeBSD is easy.  It is simply “pkg install”.  Yet, Java and Bash requires special file systems at “/dev/fd” and “/proc”.  The file “/etc/fstab” is therefore appended and two more file systems are defined.  The file systems are mounted before we proceed on the next step.

pkg install tmux bash git openjdk8
cat >> /etc/fstab << EOF
fdesc /dev/fd fdescfs rw 0 0
proc /proc procfs rw 0 0
EOF
mount /dev/fd
mount /proc

Step 2: Trying Tmux

To try tmux, one can simply issue the command

tmux

Inside the tmux session, one can:

  • Start a new window with keystrokes Ctrl-B, C
  • Switch to the previous window with keystrokes Ctrl-B, P
  • Switch to the next window with keystrokes Ctrl-B, N
  • Switch to a specific window with keystroke Ctrl-B, then the window number
  • Detach from the session with keystrokes Ctrl-B, D

For more, you can easily find information with a search engine.  The last feature, detachment, is very useful.  One can leave a program running inside a tmux session, logout, login, and reattach to the same session.  This feature is particularly useful since a minecraft server is nothing more than a long-running Java application.

Step 3: Execute the Automated Build

In your favourite working directory, download the builder Java archive (JAR) file:

curl "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar" -o BuildTools.jar

Then you can execute the JAR file:

java -jar BuildTools.jar

Step 4: Accept the EULA

After about half an hour of compilation, read the “eula.txt” and execute the following if you agree.

sed -ibak s/false/true/ eula.txt

Step 5: Tmux on Boot

Like last time with the firewall table flush, we can append a line to the “/etc/crontab” for a command to automatically start.  Yet, this time we are executing the command once only, we use the “@reboot” magic word.  In the second field, the “root” account is taken for running the program.  You have to stick with the user account you have chosen.  From the third field onward, with the “tmux new-session” command, we generate a new session.

The Java was executed with initial heap size to be 1024 MB, and maximum heap size to be 1024 MB as well.  This should be good enough unless you have a lot of friends.  In that case, you will experience some “OutOfMemoryError” and have to expand the memory accordingly.  Make sure your virtual machine is large enough to handle it.  In the folklores, it is said that one can only have heap size 60% of the total system memory.  If one has 8 GB main memory, at most the Java heap size is 4096 MB or 5120 MB.

cat >> /etc/crontab << EOF
@reboot root /usr/local/bin/tmux new-session -s spigot -d /usr/local/bin/java -Xmx1024M -Xms1024M -jar /root/spigot*.jar
EOF

Step 6: Start for the First Time

Starting it the first time is nothing much different from the command in the crontab.

tmux new-session -s spigot -d java -Xmx1024M -Xms1024M -jar spigot*.jar

Step 7: Firewall Configuration

Add port 25565 to the list of allowed ports.  In our context, we will update the line with “tcpports” in “/etc/pf.conf”:

tcpports="{22,80,443,25565}"

After that, restart the firewall with command:

service pf reload

Step 8: Connect to the Server

Open your Minecraft client, select “Multiplayer”, then “Direct Connect”.  You can then put in your server address.  If you just realise it is a commercial software, sorry, you got to pay.  Some say, one can use unauthorised Minecraft client for playing in his own server with the offline mode.  But the procedures above already assumes you run the server in online mode.

Step 9: Finding Plugins and Writing Startup Scripts

Once you get comfortable starting and stopping the servers, you can find plugins and put them to “plugins” directory.

Also, instead of calling the java command directly, you can write a shell script to encapsulate these complexities.  Sometimes server crash and you prefer it to automatically restart.  You can then add a automatic restart procedure inside the script.

#!/bin/sh
while true
do
  java -Xmx1024M -Xms1024M -jar spigot*.jar
  echo Press Ctrl-C now if you mean to stop instead of restart
  sleep 5
done

The actual procedures are left for you due to my laziness.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s