Ubuntu Linux Tips and Tricks

Some tips for Ubuntu Desktop LTS users

Upstart and Systemd script for a Minecraft server

2 Comments

I wanted to run a Minecraft server aka MCS on an Ubuntu machine that I turned into a headless server.

The tricky part is that the MCS uses Java and comes as a simple JAR file, without init script. Starting the server automatically each time the server starts would require some kind of init script.

UPDATED for systemd:

Create a file /etc/systemd/system/minecraft-server.service, as root user:

[Unit]
Description=Minecraft Home Server
After=network.target

[Service]
ExecStart=/usr/bin/sudo -iu john bash -c "cd /home/john/minecraft_server; /usr/bin/java -Xms2G -Xmx2G -jar server.jar nogui"
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

Activate the service / run the server and make it available at next start and update firewall rules:

% systemctl daemon-reload
% systemctl enable minecraft-server
% ufw allow 22
% ufw allow 25565
% ufw enable
% systemctl start minecraft-server

Old versions of Ubuntu use Upstart and Init V to start daemons/services, but I chose to try to start the MCS with upstart. Here is the contents of the upstart script: /etc/init/minecraft-server.conf (the actual JAR file minecraft_server.jar is located under /home/john/minecraft_server/)

description "Minecraft Server"

start on filesystem or runlevel [2345]
stop on run level [!2345]

env USER='john'
env APPDIR='/home/john/minecraft_server'
env APPNAME='/usr/bin/java'
env APPOPTS='-Xmx1024M -Xms1024M -jar minecraft_server.jar nogui'

expect fork

script
chdir ${APPDIR}
exec sudo -u ${USER} ${APPNAME} ${APPOPTS} &
end script

The log file of the server can be found at /var/log/upstart/minecraft-server.log (upstart takes care of that automatically for you)

The service is run as a different user john (not as root)

The service status can be retrieved with (as root):

% sudo service minecraft-server status
minecraft-server start/running, process 704

The service status can be stopped/restarted with (as root):

% sudo service minecraft-server stop
% sudo service minecraft-server start

Happy Minecrafting!

Advertisement

2 thoughts on “Upstart and Systemd script for a Minecraft server

  1. Pingback: Play Minecraft Multiplayer

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 )

Facebook photo

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

Connecting to %s