Sunday, 23 August 2015

Minecraft automatic starting, saving, and stopping

Here is a linux (Ubuntu) init script for starting and stopping (including running a /save-all to autosave) a pair of minecraft servers. Watch out for any line wrapping in the code below. Blogger has lousy support for code blocks. I've tried to keep it narrow but who knows. It's HTML. :)


#! /bin/sh
### BEGIN INIT INFO
# Provides:          minecraft
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Minecraft initscript
# Description:       Start and Stop minecraft servers
### END INIT INFO

# Author: Amos Hayes <ahayes@polkaroo.net>
#

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Minecraft Servers"
NAME="minecraft"
SCRIPTNAME=/etc/init.d/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
   screen -d -m -S minecraft_MyFirstServer \
      su - minecraft -c 'cd MyFirstServer;java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui'
   screen -d -m -S minecraft_MySecondServer \
      su - minecraft -c 'cd MySecondServer;java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui'
}

#
# Function that stops the daemon/service
#
do_stop()
{
        screen -S minecraft_MyFirstServer -p 0 -X stuff "/save-all$(printf \\r)/stop$(printf \\r)"
        screen -S minecraft_MySecondServer -p 0 -X stuff "/save-all$(printf \\r)/stop$(printf \\r)"
}

case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  restart)
        #
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        #
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        #echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
        echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
        exit 3
        ;;
esac

:

Modern Ubuntu web kiosk using chromium as the browser engine

 I have been working to prepare a digital atlas exhibit for the Natillik Heritage Centre in Gjoa Haven, Nunavut, Canada. Working with Indig...