Pages - Menu

Saturday, April 21, 2012

starting minecraft server on system startup

This is an initscript to run a Minecraft or CraftBukkit server on CentOS, Fedora, and Ubuntu.

Features
  • Start, stop, restart CraftBukkit as a system service
  • Automatic (via cron) and manual logfile rotation
  • Automatic (via cron) and manual backups
  • Backup compression and rotation (keeps 7 days worth of backups)
  • Check latest Recommended Build and update to it if required
  • Information display including Java path, current memory usage, current TCP connections
  • Able to run multiple separate instances of the server at once
Supported Distributions
  • CentOS 6, CentOS 5, Fedora 14
    (probably works on Fedora Core 6 and later, untested)
  • Ubuntu Server 12.04 LTS
Other distros which use SysV Init or Upstart (Debian, Mint) will probably work.
Distros using systemd (Fedora 15+, Arch Linux, etc) will not work.

Get the script, view Requirements, Installation, Backups, Multiple Instances, and Usage
License

Released under GNU GPL v3 - http://www.gnu.org/licenses/

Credits

Thanks to these people whose work I have used in the making of this

29 comments:

David Catalano said...

Well done! Precisely what I was looking for. I'll be monitoring Github for updates. Wasn't clear if/how the "update" function would work. I'll be modifying this for Tekkit so that CraftBukkit URL won't be applicable.

thanks for sharing

JD said...

This is wonderful to find! Thank you for putting the work into documenting this clearly.

I am having some trouble with it, though. The commands minecraft status minecraft info and minecraft update work for me, but not minecraft start, stop, restart, backup, or console.

When a command fails, I get a variation of this message, making me think the problem is with systemctl permissions: Starting minecraft (via systemctl): Failed to issue method call: Access denied [FAILED]

Do you know how to resolve this? I am new to systemctl as I just changed from ubuntu over to Fedora Core.

Jamie said...

The script is written for SysV init, I wouldn't expect it would work with systemd. I'll have to re-write for systemd and probably split the script into two parts, as systemctl only supports start/stop/status commands.

I do the development and testing on CentOS 6, that's probably your best bet, I've also tested on CentOS 5.

Fedora isn't really suitable for a server OS, due to its short release lifespan and focus on providing up-to-date desktop software.

If you do wish to use Fedora, the script should work on Fedora 14 and earlier (that is documented in the Requirements section above). I believe it also works on Ubuntu and Debian, and basically any distro with SysV init or upstart.

I probably won't think about supporting systemd until the second half of 2013, when systemd itself is much more mature and CentOS 7 is close to release.

JD said...

Jamie, thank you for your advice. I installed CentOS and it was a much smoother experience than Fedora 17. It all works except for telling the number of active connections. This is great!

Jamie said...

Glad to hear things are much better!

The "minecraft info" won't give you a number of active connections, it will just show every connection on the server port including the listening port.

If you run the server on a different port, I've just added functionality to handle that. You can fetch the latest from github to update.

Jamie said...

If anyone is following this post, Ubuntu Server 12.04 is now supported.

Anonymous said...

Thanks!

Unknown said...

There's a bug in your logrotate code - it's assuming there is no period in the path other than in the filename. I've got /home/minecraft/1.4pre as my home directory, so the cut doesn't work.

Solution: use basename $i instead of ls $i before doing the cut. You don't need the second cut, either, as you already know the name of the file is server.log (in fact, you're counting on it).

Jamie said...

Thanks Sarah, I finally got around to adding this.

Shawn said...

I am getting:
shawn@fortress /home/bukkit/craftbukkit $ minecraft console
cat: /home/bukkit/craftbukkit/server.properties: Permission denied
cat: /home/bukkit/craftbukkit/server.properties: Permission denied
Password:

when trying to do a minecraft status, any ideas? I'm newish to linux, I feel it could be related to perms on the /home/bukkit/craftbukkit but I have:
-rw----rw- 1 bukkit shawn 593 Mar 4 11:34 server.properties
it should work right???

Jamie said...

It seems you are logged on as user "shawn" but your Minecraft user is "bukkit"? Log on as the "bukkit" user or as root to manage the service.

Barron Eskind said...

Hey aleph. This is a great post!

I'm still having issues with log rotate.

Did as root in /etc/init.d/
wget https://raw.github.com/superjamie/minecraft-init-script/master/minecraft

Edited username to be barron
Edited MCPATH="/home/barron/mcserv/bin"
Edited BACKUPPATH="/home/barron/mcserv/prev"

Now. When I manually enter "minecraft logrotate" as root or barron I get this error.

/bin/cp: Cannot create regular file `/home/barron/mcserv/bin//home/barron/mcserv/bin/server.log.1': No such file or directory

And the weird ` was in there too. Idk. I've poured over the logrotate "s and can't see anything funny.

Jamie said...

Hi Barron. Do you actually have a logfile at $MCPATH/server.log yet? I'm not sure what would happen if logrotate was run without that. I'm guessing the backtick might be a slight typo, could I bother you to "cat /etc/init.d/minecraft" and paste the contents into www.pastebin.com so I can see? Cheers, Jamie

Barron Eskind said...

I do have a server.log file. It's good and full until I run Minecraft logrotate. Then it's empty :(. I'll get back to you on the cat Minecraft. My server is tied up right now.

Barron Eskind said...

Hey Jamie, thanks for your patience, here is the /etc/init.d/minecraft cat

http://pastebin.com/F3h6rb5e

Barron Eskind said...

I've finally found the problem:!!!

Lines 303 - 306 read
LOGTMP="$MCPATH/server.log"
LOGNEW="$LOGTMP.1"
as_user "/bin/cp $MCPATH/server.log $MCPATH/$LOGNEW"

SHOULD BE:
LOGTMP="$MCPATH/server.log"
LOGNEW="$LOGTMP.1"
as_user "/bin/cp $MCPATH/server.log $LOGNEW"

because LOGNEW already includes $MCPATH

Jamie said...

I didn't get a chance to look at this until today sorry. Thank you very much for finding the bug! I've implemented the fix in a slightly different way, but the result should be the same. Cheers!

Unknown said...

The script is working perfectly but it always ask me for a password. If I want to restart my serveur, i need to write my password 5 times... is it normal?
What is wrong?

login as: bukkit
bukkit@XXX.XXX.XXX.XXX's password:
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.54-2 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Mar 25 17:29:37 2014 from XXX.XXX.XXX.XXX
$ /etc/init.d/minecraft restart
/etc/init.d/minecraft: 21: [: 1: unexpected operator
* SERVEUR MINECRAFT is running (pid 3009). Commencing shutdown...
* Notifying users of shutdown...
/etc/init.d/minecraft: 40: [: bukkit: unexpected operator
Password:
* Saving map named "MAP" to disk...
/etc/init.d/minecraft: 40: [: bukkit: unexpected operator
Password:
* Stopping SERVEUR MINECRAFT...
/etc/init.d/minecraft: 40: [: bukkit: unexpected operator

Jamie said...

It looks like there are some errors on line 21 and line 40. Can you put your script into a pastebin like pastie.org?

Unknown said...

Thanks

My script :

http://pastie.org/8971019

I only change the first line :

SETTINGSFILE="/home/bukkit/minecraft/settings"

Jamie said...

Is that the entire file? You need the bits at the start too, the shebang and the comments and the functions.

I'd also suggest keeping the settings file in /etc/default/minecraft, I haven't tested moving it into home directories, that kinda conflicts with the idea of a system-wide initscript.

Unknown said...

Thanks, every things is working vert well but i have one more problem, i need to do "exec bash" when i login from putty each time. How can i solve this?

Jamie said...

That sounds more like a problem with your login settings or shell the in your user account. I'd suggest asking on a Debian forum.

Unknown said...

I am running Xubuntu 14.04 and attempting to use your script but I run a vanilla server not a bukkit so can I just comment out the line about the URL of the server exectuable for update checking?

Unknown said...

another question, i try to run "minecraft start" and it says command not found but if I issue "/etc/init.d/minecraft start" it works. and yes I did add the alias and restarted my machine. Next issue is that it's not starting the minecraft when I boot the machine. I am running Xubuntu 14.04, I followed your steps using update-rc.d minecraft defaults. Another issue is that when I try to stop the server it fails to stop it using the script, it returns [ERROR] Ubucraft is still running (pid 5714) Could not be shutdown. Any help would be appreciated.

Unknown said...

sorry, one last question. in your java command what is the -Djava.net.preferIPv4Stack=true for and do I need that? Because when I run /etc/init.d/minecraft start it appears to start but i can't see the server when I launch a minecraft client, it's as if the server isn't online despite minecraft status showing that its running. Thanks a lot if you can help me.

Jamie said...

Yes, you can just use the original vanilla server and URL, or you can just leave the URL. I haven't tested on 14.04 yet. The IPv4 command binds to IPv4 instead of IPv6, which is probably faster considering the low penetration of IPv6 over the internet, as an IPv6 route is attempted first and then falls back to IPv4.

Also there are new installation instructions on GitHub, so ensure you're following the instructions there, not here on the blog. I'll update this post to say the same.

Unknown said...

I have followed the instructions within the README at github and minecraft still does not start when I reboot my machine. As I said before i am using Xubuntu 14.04 so maybe update-rc.d minecraft defaults doesn't add the script to be started within 14.04. Even when I run it manually after switching users to the minecraft user it doesn't show up as connectable on my LAN. When I look at ps aux | grep minecraft I see two instances of the command but when I run minecraft status is says it's running. THen if I try to stop the server using minecraft stop it fails to stop the server. If you had skype and wouldn't mine helping me i'd really really appreciate it otherwise i understand

Jamie said...

Sorry I'm not available for live support. I'll try to have a look at the script on Ubuntu 14.04 some time over the next couple of weeks.