|

CentOS 7 Services (systemd) and Minecraft Servers

So last night I finally got around to configuring my minecraft servers to start with the OS. The server hosting my minecraft servers is running CentOS 7. I run CentOS because Red Hat 9 was my first linux OS and I have since preferred RPM based systems over DEB based. Also CentOS is closer to what most enterprise environments run anyways.

I am very familiar with the older style of init scripts for starting and stopping the process. But CentOS 7 switched to systemd units. I thought about just enabling execute for rc.local but decided against it. I am about to start a position where I will be working in a 95% linux environment. So I figured I could use the practice.
My first lazy attempt was to pass the minecraft init script to Execstart and Execstop. It repeatedly failed to properly execute. “Error 203” Most of the info I came across was either regarding making sure the file had execute permissions or a missing #!/bin/bash or #!/bin/sh

That was not my case and after spending 2 or 3 hours going in circles on this I moved on to writing my own unit. I had already started looking at the other services like sshd.service located in /usr/lib/systemd/system to get an idea of the format as well as looking up the various service types. I tried changing the type to forking as the default minecraft init script uses screens to run and access the background process but this would be a forked process.
As I was reading more and more about systemd, the added security and features are really nice. But CentOS isn’t using the latest version of systemd so I don’t have all the features. 

Below is my final unit that finally worked.

[Unit]
Description=Minecraft Server with Tekkit mods
After=network.target
Wants=network.target

[Service]
User=minecraft
Group=minecraft
PIDFile=/servers/tekkit/service.pid
KillMode=none
SuccessExitStatus=0 1
NoNewPrivileges=true
PrivateTmp=true
InaccessibleDirectories=/root /sys /srv /opt /media /lost+found
ReadWriteDirectories=/servers/tekkit
WorkingDirectory=/servers/tekkit

ExecStart=/servers/tekkit/launch.sh
ExecStop=/servers/tekkit/stop.sh

[Install]
WantedBy=multi-user.target

And here are the two referenced shell scripts. Note that full paths to the executable are needed as there is no $PATH variable set in the shell that runs the scripts.

#!/bin/sh
# This the launch.sh Script

/usr/bin/java -server -d64 -Xms3G -Xmx3G -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+DisableExplicitGC -XX:+AggressiveOpts -XX:+UseFastAccessorMethods -XX:+UseBiasedLocking -XX:TargetSurvivorRatio=90 -XX:+CMSIncrementalMode -jar TekkitLite.jar nogui
#! /bin/bash
#
# This is to properly shutdown the Tekkit Server
# This will provide a 15 second warning to users
# before saving and shutting down the server safely.
#
# Written by David Trask
# Date: 07/25/2016
#
/servers/tekkit/mcrcon -H localhost -P 2560 -p password say Server is shutting down in 15 seconds
/servers/tekkit/mcrcon -H localhost -P 2560 -p password save-all
sleep 15s
/servers/tekkit/mcrcon -H localhost -P 2560 -p password stop
sleep 10s

For stopping the server I am using mcrcon which connects to the rcon service attached to the minecraft server. This must be enable in the servers.properties file for the minecraft server. The .service file was created in /usr/lib/systemd/system

Short and sweet but I learned a lot about about the systemd and I might setup a debian server just to play with the newer systemd….