Saturday, November 15, 2014

Ubuntu - Starting Minecraft Server Automagically FAILs

I run a Minecraft server for myself and some close friends. One thing that I struggled with was getting Minecraft to auto-start when the machine booted up. This is important so that if my power is ever interrupted (not on a UPS, yet.....) the computer would auto-restart via the UEFI bios setting which would then auto-start the Minecraft server due to the init script being added to the default system run levels from using sudo update-rc.d defaults. The error within /var/log/boot.log was
Cannot make directory '/var/run/screen': Permission denied

When I googled the error message I found that it's a bug, apparently screen-cleanup is running via upstart much earlier than it expects to have run, and is failing to correctly clean up the /var/run/screen directory. I'm not sure whether this is an acceptable fix or not but I found a solution after much googling and reading. Basically we're not going to rely on the screen-cleanup upstart or init script, we're going to remove that upstart job completely and within our Minecraft init script we'll create the /var/run/screen directory ourselves. I am using this minecraft init script which is right from the Minecraft wiki page and in order to get around this Ubuntu screen bug we need to make a small modification to it. Note that this modification should work for any init script you're using to auto-start your Minecraft server if you have the same screen bug in Ubuntu. The applicable section looks like the following
cd $MCPATH
as_user "cd $MCPATH && screen -h $HISTORY -dmS minecraft $INVOCATION"
sleep 7

And we need to add the commands for making the /var/run/screen directory ourselves so make it look like this
cd $MCPATH
if ! test -d /var/run/screen; then
mkdir /var/run/screen
chown root:utmp /var/run/screen
chmod 775 /var/run/screen
fi
as_user "cd $MCPATH && screen -h $HISTORY -dmS minecraft $INVOCATION"
sleep 7

It's important that after you alter your Minecraft init script you run the following commands to remove it from the various run levels and then re-add it. (I don't know if remove, defaults, and enable are ALL needed but that's what I did.
sudo update-rc.d minecraft remove

sudo update-rc.d minecraft defaults

sudo update-rc.d minecraft enable

Now when the computer starts up from it being off, a screen session will get created and your Minecraft server will get auto-started from within that screen session. You can then ssh into your server as user minecraft and issue
screen -r

and it will reattach you to your Minecraft server console. Hopefully this was helpful for you as I spent many hours trying to figure out why the init script was not working to auto-start the Minecraft server when the machine was turned on. -Ubu out

No comments: