I’m pretty sure that by now, everyone reading this blog has heard of or probably even played Minecraft himself. It’s a sweet little game that tosses you into a simplified world that you can freely (and easily!) shape in any way you want. Your tasks: feed yourself, keep monsters out of your buildings and build something remarkable.
Minecraft can, of course, be played with any number of players online. All you need is a server with shell access and Java on it. If you also have Mono on your server, you can use McMyAdmin, a very convenient web frontend for the Minecraft server to manage users and perform automated backups.
Now if you’re running a server for longer than just an evening game, you’d probably want it to keep running even when you log off. This can be accomplished via the GNU Screen utility which runs a process in a virtual console and can keep it running even when the user that launched it logs off.
As any self-conscious server administrator would want to do, you of course want your Minecraft server to run as a daemon so it can be scheduled to automatically quit when the system is shut down and start again when the system boots up. What you want is an init script!
Gentoo
I have found some init scripts for a pure Minecraft server, but not many for McMyAdmin, so I had to write one myself. I’m running Gentoo Linux, the only good Linux distribution there is, so obviously this init script is meant for that distribution as well:
#!/sbin/runscript
# Init script for McMyAdmin on Gentoo Linux
# Written by Markus Ewald, public domain, use on your own risk.
#
depend() {
use net ypbind nis
after slapd mysqld postgresql
}
start() {
PWHOME="$(getent passwd $USER | awk -F: '{ print $6 }')"
ebegin "Starting minecraft"
env TERM="xterm" \
start-stop-daemon \
--start \
--make-pidfile \
--pidfile /var/run/minecraft.pid \
--background \
--user $USER \
--env HOME="${PWHOME:-/home/$USER}" \
--name minecraft \
--chdir /opt/minecraft \
--exec /usr/bin/screen -- -D -m -S minecraftd /usr/bin/mono McMyAdmin.exe
eend $?
}
stop() {
ebegin "Stopping minecraft"
screen -p 0 -S minecraft -X eval 'stuff /quit\015'
sync
sleep 3
if [ -e /var/run/minecraft.pid ]; then
read PID < /var/run/minecraft.pid
if [ -d /var/run/${PID} ]; then
echo Normal shutdown not successful, forcing...
start-stop-daemon \
--stop \
--signal 15 \
--pidfile /var/run/minecraft.pid
fi
fi
eend $?
}
Paste this as /etc/init.d/mcmyadmin and make it runnable (chmod +x /etc/init.d/mcmyadmin), then you can start and stop your McMyAdmin-based server like any other system service.
If you're new to screen, in order to view the console output of McMyAdmin, just enter screen -r to connect to the virtual screen console. To put it back in the background, press Ctrl+A,D (or enter /quit to terminate McMyAdmin).
Debian
Debian users should download the Gentoo Live CD, boot up and follow the Installation Guide in the Gentoo Linux Handbook. Oh well, if you really must, you could of course also install those 3 years old Mono packages, revel in having half of the X windows system on your headless server and use this init script instead:
#!/bin/bash
# Init script for McMyAdmin on Debian Linux
# Written by Markus Ewald, public domain, use on your own risk.
#
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Minecraft server
# Description: Starts the minecraft server
### END INIT INFO
# Settings
USER='minecraft'
mc_start() {
echo "Starting McMyAdmin/Minecraft server..."
env TERM="xterm" \
start-stop-daemon \
--start \
--make-pidfile \
--pidfile /var/run/minecraft.pid \
--background \
--user $USER \
--name minecraft \
--chdir /opt/minecraft \
--exec /usr/bin/screen -- -D -m -S minecraftd /usr/bin/mono McMyAdmin.exe
echo "I'm optimistic that McMyAdmin/Minecraft might be running now."
}
mc_stop() {
echo "Stopping McMyAdmin/Minecraft server"
screen -p 0 -S minecraft -X eval 'stuff /quit\015'
sync
sleep 3
if [ -e /var/run/minecraft.pid ]; then
read PID < /var/run/minecraft.pid
if [ -d /var/run/${PID} ]; then
echo "Normal shutdown not successful, forcing..."
start-stop-daemon \
--stop \
--signal 15 \
--pidfile /var/run/minecraft.pid
fi
fi
echo "There's a good chance that McMyAdmin/Minecraft is stopped now."
}
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
*)
echo "Usage: /etc/init.d/minecraft {start|stop}"
echo " ...and hope that it works!"
;;
esac
I tested this on exactly one Debian system. It worked there.
Update: A user by the name of bitstacker has given the Debian script some love, check out his blog post (in German) or download the updated script from GitHub. Cheers!