Thursday, March 19, 2015

Automatically Start Local Minecraft Server on Linux (Fedora) At Boot

Running your own Minecraft server on Linux is pretty simple.  You simply download the jar file from and run it from the command line.  However, if you run it straight from the command line you'll need to stay logged in and not kill the terminal otherwise it will stop the Minecraft server.

The way to work around it is to use something like tmux or screen and run Minecraft from there.  That allows you to detach the session, logout, and come back to it at a later time so you now have a "headless" Mindcraft server running.

The final step is to make it so that it automatically starts when the server boots up and shut it down when the server shuts down.  On Fedora, that means using systemd.

I'm assuming you're running Minecraft as user minecraft and the Minecraft jar file is located in ~minecraft.

First, let's create a start-up script in $HOME/

/usr/bin/tmux new-session -s minecraft -d
tmux send -t minecraft "/usr/bin/java -Xmx1024M -Xms1024M -jar $HOME/minecraft_server.1.8.3.jar --nogui" ENTER
This starts a new tmux session in detached mode and calling the session minecraft.  Then it sends to the tmux session the command to start the Minecraft server in text (non-gui) mode.

Let's make a shutdown script in $HOME/minecraft/

/usr/bin/tmux send -t minecraft /save-all ENTER
/usr/bin/tmux send -t minecraft /stop ENTER
echo "Killing minecraft session"
/usr/bin/tmux kill-session -t minecraft
This sends a command to the tmux session named minecraft to
  1. Save the current state of the server (save-all).
  2. Shutdown the minecraft server (/stop).
  3. Stop the tmux session.
Make both scripts executable:

chmod u+x
chmod u+x
To automatically start and stop the tmux session for Minecraft, create /usr/lib/systemd/system/minecraft.service:

Description=Start tmux in detached session running Minecraft.


Now link it to the right place:

cd /etc/systemd/system
ln -s /usr/lib/systemd/system/minecraft.service minecraft.service
Finally, you want to start it and enable it to start at boot:

systemctl start minecraft.service
systemctl enable minecraft.service
I also suggest having a backup script that regularly back up your world:

printf "Starting backup..."
date +%D
cd /home/minecraft
tmux send -t minecraft /save-off ENTER
tar -czvf backup/world-`date +%m%d%y_%H_%M_%S`.tar.gz world
tmux send -t minecraft /save-on ENTER
With a cron job as follows:

0 15 * * * /home/minecraft/ >> /home/minecraft/backup/backup.log 2>&1

This says to run the job at 3pm every day and copy the output to backup.log.

The saved archive (even compressed) can get pretty big and can easily eat up disk space so you might want to only keep the a few of the most recent save and automatically delete the old ones (or move old archives somewhere else).  That can be done with another cron job with a one line script:

ls -tr world-*.tar.gz | head -n -5 | xargs --no-run-if-empty rm
This will delete all but the 5 most recent files and do nothing if there are less then 5 archive files.  This can be put into a script with some logging /home/minecraft/

printf "Deleting old backups..."
date +%D
cd /home/minecraft/backup
ls -tr world-*.tar.gz | head -n -5 | xargs --no-run-if-empty rm -v
and have a cron job as follows to run nightly:

# Delete old backup files at 11:20pm but keeping a few most recent ones.
20 23 * * * /home/minecraft/ >> /home/minecraft/backup/backup.log 2>&1

Now each time your server boots, it will automatically run the Minecraft server as user minecraft and once a day it will back up your Minecraft data while removing old archives.

No comments:

Post a Comment