Code Monkey home page Code Monkey logo

minecraft-init-script's Introduction

minecraft-init-script

by Jamie Bainbridge <[email protected]>

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

Note

As of 2015, this script is no longer actively maintained. All the supported distros are moving to systemd, making the concept of an LSB initscript rather outdated. All issues I see are related to bugs in Ubuntu's implementation of Upstart, it works perfectly on CentOS. However I think you are much better learning to use containers like Docker to control a single process and its files. There are many instructions around the internet on how to do this.

Thanks to all supporters over the years. You are welcome to fork this repo if you wish to continue development.

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)
  • Allows use of third-party backup solutions
  • 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 will probably work.

Distros using systemd (Fedora 15+, Arch Linux, etc) may not work.

Requirements

  • screen, rsync

    (you may need to install these)

  • bash, chkconfig or sysv-rc, coreutils, cronie, curl, diffutils, grep, initscripts, net-tools, procps, tar

(these should all be installed by default)

  • Oracle Java 7

  • Enough disk space to save your map twice, plus another ~5 times for a week of compressed backup space.

ie: If your map is 1Gb then you probably need at least 7Gb, plus any space your plugins require and any additional backups you'll be making.

Installation

As the root user:

  • Install Sun Java (CentOS/Fedora)

    Download the RPM from http://www.java.com/

    yum localinstall jre-<version>.rpm

  • Install Sun Java (Ubuntu)

    sudo add-apt-repository ppa:webupd8team/java
    sudo apt-get update
    sudo apt-get install oracle-java7-installer
    
  • Confirm your JVM installation

    java -version
    
  • Create a new user with a home directory

    useradd -m bukkit
    
  • Save the script as /etc/init.d/minecraft and make it executable

    chmod +x /etc/init.d/minecraft
    
  • Copy between the <<COMMENT and COMMENT lines and place the copy at /etc/default/minecraft

    If you need to edit settings, edit the /etc/default/minecraft file, not the initscript

  • Allow the bukkit user to run the init script without needing root access

    Type visudo and add this line to the bottom:

    bukkit localhost=NOPASSWD:/etc/init.d/minecraft*
    
  • Create an alias so you only have to type minecraft to run the script

    Add the following line to both root and bukkit's ~/.bashrc file:

    alias minecraft='/etc/init.d/minecraft'
    
  • Start the server on system boot if desired (CentOS/Fedora)

    chkconfig --add minecraft
    chkconfig minecraft on
    
  • Start the server on system boot if desired (Ubuntu)

    update-rc.d minecraft defaults
    

As the regular user, bukkit:

  • Make the required paths

    mkdir -p ~/backups && mkdir -p ~/craftbukkit
    
  • Put your craftbukkit.jar, world, plugins, server.properties, etc into ~/craftbukkit

Backups

  • Create cron jobs to do regular backups and rotations around 4am

    Type crontab -e to open the cron interface and add the following

     0 4 * * * /etc/init.d/minecraft backup              # backup world at 4:00am
     5 4 * * * /etc/init.d/minecraft logrotate           # rotate logs at 4:05am
    15 4 * * * /etc/init.d/minecraft removeoldbackups    # remove old backups at 4:30am
    
  • If you have multiple worlds, you can pass the worldname as a parameter to the regular backup

     0 4 * * * /etc/init.d/minecraft backup world1       # backup world1 at 4:00am
     5 4 * * * /etc/init.d/minecraft logrotate           # rotate logs at 4:05am
    15 4 * * * /etc/init.d/minecraft backup world2       # backup world2 at 4:15am
    30 4 * * * /etc/init.d/minecraft removeoldbackups    # remove old backups at 4:30am
    
  • If you wish to use a third-party backup solution, just disable world writes

    minecraft save-off
    

    Then run your backup tool. Then re-enable world writes

    minecraft save-on
    

Multiple Instances

It is possible to run multiple instances, for example a Creative server and a Survival server, on the same system.

  • Copy the script to two new files

    cp /etc/init.d/minecraft /etc/init.d/minecraft-creative
    cp /etc/init.d/minecraft /etc/init.d/minecraft-survival
    
  • Edit the Provides section on Line 6 to the same as the new filename

    # Provides: minecraft-creative
    # Provides: minecraft-survival
    
  • Create a settings file for each instance in /etc/default/ using the same name as the script

    /etc/default/minecraft-creative
    /etc/default/minecraft-creative
    
  • Set an alias for each server in ~/.bashrc

    alias creative='/etc/init.d/minecraft-creative'
    alias survival='/etc/init.d/minecraft-survival'
    
  • Add the new scripts to chkconfig or update-rc.d

  • Set the paths of the separate maps in each script

    MCPATH="/home/bukkit/craftbukkit-creative"
    MCPATH="/home/bukkit/craftbukkit-survival"
    
  • Change your screen session names in each script

    SCRNAME="creative"
    SCRNAME="survival"
    

Usage

  • Start the server

    minecraft start
    
  • Stop the server

    minecraft stop
    
  • Restart the server

    minecraft restart
    
  • Back up the map and executable

    minecraft backup
    

    The final compressed backup is done when the .md5 file appears in the backup directory.

  • Back up multiple maps

    minecraft backup world1
    minecraft backup world2
    
  • Check the server is running

    minecraft status
    
  • Get some more info

    minecraft info
    
    * CraftBukkit (pid 9037) is running...
    - Java Path : /usr/java/jre1.6.0_31/bin/java
    - Start Command : java -Xms512M -Xmx3584M -jar craftbukkit.jar nogui
    - Server Path : /home/bukkit/craftbukkit
    - World Name : world
    - Process ID : 9037
    - Memory Usage : 742796 kb
    - Active Connections :
    Proto Recv-Q Send-Q Local Address Foreign Address State
    tcp 0 0 0.0.0.0:25565 0.0.0.0:* LISTEN
    tcp 0 0 192.168.2.99:25565 192.168.2.69:55507 ESTABLISHED
    
  • Broadcast a message to the server

    minecraft say
    

    Note that some punctuation like 'apostrophes' will not work.

License

GNU GPLv3

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Contributors

Initial concept based off

minecraft-init-script's People

Contributors

occanowey avatar superjamie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minecraft-init-script's Issues

Multiple server support

As it is mentionned in the title, it would be great to have a multi-server startup script.
Actually copying the script for each server raise the maintenance costs.

Improve implicit sleeps

The script implicitly sleeps for a set amount of time, assuming that time is enough.

A better way would be to sleep in 1-second loops with a bound on the loop, eg:

+    C=0
+    while [ $C -lt 15 ]; do
+      C=$[C+1]
+      echo -n "."
+      if server_running
+      then
+        echo
+        echo " * [OK] $SERVERNAME is now running (pid $JAVAPID)."
+        exit 0
+      else
+        sleep 1
+      fi
+    done

Thanks to Jon Stephens for this idea.

SETTINGSFILE gets wrong name on system boot

I realise this script has been retired. Just thought I'd leave a note for those who come after me and have the same problem.

On RHEL-derived systems at least (don't know about Ubuntu etc.), the init script can fail to start on system boot. The reason is the following line (110)

SETTINGSFILE="/etc/default/$(basename $0)"

On boot, this resolves to something like /etc/default/S85minecraft, not /etc/default/minecraft. Since this file doesn't exist, the server doesn't start.

centos 6 error

on centOS 6 i the following error when i run chkconfig --add minecraft.

error reading information on service minecraft: No such device

integer expression expected

When i run my server and to the info command i get in the middle of the info this line:
/etc/init.d/minecraft line 444: [: : integer expression expected

Running CentOS 6.4 min.
Java JDK 1.7.0_45

Script error

I get the following errors when calling the script:

[bukkit@localhost ~]$ minecraft start

  • CraftBukkit was not already running. Starting...
  • Using map named "world"...
    bash: line 1: -XX:MaxPermSize=128M: command not found
    bash: line 2: -XX:+UseParallelOldGC: command not found
    bash: line 3: -XX:+AggressiveOpts: command not found
  • Checking CraftBukkit is running...
  • [ERROR] Could not start CraftBukkit.
    [bukkit@localhost ~]$

Not Starting with Ubuntu.

Hello.
(I'm sorry for my English, I'm french)
Thank you for this script, but it didn't start with Ubuntu. It works with a manual command ("minecraft start").
Have you an Idea ?

Settings file not working

I cant seem to get the settings file to work. i am running centOS 6 and i copy between the lines and paste it in a new file at /etc/default/minecraft. i run minecraft start and i get these errors:

[root@localhost ~]# minecraft start
: command not foundaft: line 2:
: command not foundaft: line 5:
: command not foundaft: line 8:
: command not foundaft: line 11:
: command not foundaft: line 14:
: command not foundaft: line 17:
: command not foundaft: line 20:
: command not foundaft: line 23:
: command not foundaft: line 26:
: command not foundaft: line 29:
: command not foundaft: line 31:
: command not foundaft: line 34:
: command not foundaft: line 38:
: command not foundaft: line 41:
: command not foundaft: line 44:
: command not foundaft: line 48:

  • [ERROR] Settings file /etc/default/minecraft is not applying. Can't run!
    Check your options are uncommented and you haven't copied the <<COMMENT block.

the lines seem to be where ever there is a blank space so i removed the spaces and i get this:

[ERROR] Settings file /etc/default/minecraft is not applying. Can't run!
Check your options are uncommented and you haven't copied the <<COMMENT block.

here is my settings file

image

[ERROR] Could not start MC Test Server.

I am fairly certain the error is from:

if server_running
then
echo " * [OK] $SERVERNAME is now running (pid $JAVAPID)."
else
echo " * [ERROR] Could not start $SERVERNAME.
exit 1
fi

I have gone through it several times and made sure that it is formatted correctly and as far as I can tell it is. The logic seems to be solid as well.

Help, please, if you are still paying attention to this.

Thanks!

Add "minecraft say" support

Add in lorenzorota's "minecraft say" support from

Super Jamie, I created a new case statement for the initscript which I thought would be pretty useful for people who are not so familiar with the MC console or just prefer not using it.

So I added this function:

## Broadcasts a message something

mc_say() {
    s_string="${@:1}"
  if [[ -z "$s_string" ]]
  then
    echo " * You need to enter your message. Usage; \"minecraft say message\""
  elif server_running
  then
    echo " * Broadcasting \"$s_string\""
    as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"say $s_string\"\015'"
  else
    failure && echo " * $SERVERNAME was not running!"
    exit 1
  fi
}

Created this case statement:

  say)
mc_say ${@:2}
;;

And well... I also edited the usage display list:

 echo " * Usage: minecraft {start|stop|restart|backup|update|status|info|console|say}"

My version may be rather a bit clumsy, but I just wrote it as a small script which would allow me to easily create crontab jobs with adjustable messages such as:

0 0 25 2 * /etc/init.d/minecraft say Today is Lorenzo's Birthday, Let's party and eat some cake WOO!

or even displaying the time every 10 minutes:

*/10 * * * * /etc/init.d/minecraft say The time right now is: $(date +"%r")

When using multi-map backups, one "removeoldbackups" removes the backups for all maps.

When using multi-map backups, one "removeoldbackups" removes the backups for all maps.

For example, if the backup folder is being used for "creative" and "survival" maps, then one "removeoldbackups" removes the old files for both "createive" and "survival".

Ideally, "removeoldbackups" should just remove its own backups.

Look into how this affects removing old executables as well. We don't want to fill the hard drive with backups of craftbukkit.jar or whatever.

The Screen plays Hide & Seek

Hello guys !
I've got a problem, your Script can't start with my OS (Ubuntu Server 14.04) !
We can see this error in boot.log :
Cannot make directory '/var/run/screen': Permission denied

Google thinks that I use :
sudo /etc/init.d/screen-cleanup start
If I add this line in your Script, it works ! BUT : The Screen doesn't exist !!!
Why ?!?
And if I add : sleep 1, we have the same problem ?
What do you think ? Have you an Idea ?
Thanks.

BungeeCord server support

I would be great to also support BungeeCord proxy.

It should not be hard, maybe using a flag to tell that you run Bungee to limit some commands.

Take out log file logic

Upstream CraftBukkit now rotates and compresses its own logs into logs/latest.log and logs/YYYY-MM-DD-#.log so just remove all the logfile handling from the script.

[ERROR] Could not start CraftBukkit

Hello! This is a fine script.

However.. I would like to find out what is stopping me from starting up the service. When I run service minecraft start I get the following:

* CraftBukkit was not already running. Starting...
 * Using map named "world"...
 * Checking CraftBukkit is running...
 * [ERROR] Could not start CraftBukkit.

Of course that's not very helpful. I can run the .jar file from the command line without any problems, and I have replaced the invocation for a simple one as: java -Xmx1024M -jar craftbukkit.jar -o true and still no luck :(

Is there any way to get a more verbose output and find out what's going on?

I'm running this on Ubuntu 13.10 with CraftBukkit 1.6.4-R2.
Thanks!

Use tmux instead of screen

It seems GNU Screen has issues starting during Ubuntu's broken init implementation. Maybe using tmux could work around that just cos it's different?

Plus, screen is old and tmux is new.

I do have a concern that upstream tmux has not stabilised on their command set yet, some config variables did change between v1.6 (EPEL6) and v1.9 (Fedora 20). I assume the supported Ubuntus have similar variety of versions, so I'd need to look into all of those.

Thanks to Jon Stephens for supplying changes for this.

Move settings out to a file in /etc/sysconfig to enable easy script updates

Currently when users update the script, they have to manually put all their settings in again. This is inconvenient and annoying.

A settings file in /etc/sysconfig/minecraft would be a better solution to this. The settings and script could be separate, and users could update the script without the need to change the settings.

Ubuntu 14.04 Issue

Get this when I do 'minecraft console'

"Cannot open your terminal '/dev/pts/0' - please check

Backup issue

If backup dir does not exist, it should be created.

No automatic start/stop on Ubuntu

When an Ubuntu 12.04.4 LTS server starts, the Minecraft server doesn't start automatically.

On shutdown, the following error is printed:

[error] settings file /etc/default/K20minecraft does not exit. Can't run !

Starting and stopping the service manually post-boot works.

This can be worked around by changing:

SETTINGSFILE="/etc/default/$(basename $0)"

to:

SETTINGSFILE="/etc/default/minecraft"

Password Twenty Times For Commands?

This init script works really nice, except I can't open up the console. I do "minecraft console" and I get in return "Cannot open your terminal '/dev/pts/0' - please check." Is this me or the script?

It also seems to make me type in my password so many times for any command, but that might be my fault.

Any interest in backup to AWS S3 feature?

I've got a separate script that performs offsite backups to Amazon Web Services S3 on my server. Is this a feature that people want? Would a pull request be looked on favourably?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.