Service control script: init.d/minecraft

Configuration file /etc/default/minecraft

VariableDescriptionDefault value
MINECRAFT_USER_NAMEThe user name to run Minecraft serverminecraft
MINECRAFT_HOMEHome directory of Minecraft server instances/home/minecraft
MINECRAFT_START_COMMANDServer startup commandexec sh start.sh
MINECRAFT_USE_TMUXEnable to uses of tmux(1)NO
MINECRAFT_SESSION_NAMEBase session name of tmux(1) sessionsminecraft
MINECRAFT_ALTERNATIVE_LOG_FILERedirect outputs to this file if tmux(1) is not used/dev/null
MINECRAFT_SERVERSSpace separated server instances

Server instances are directories under MINECRAFT_HOME; each server instance will be started under its instance directory by running MINECRAFT_START_COMMAND. Enabling tmux(1) is recommended if using Bukkit server framework, as this server didn't save worlds when exiting on signals. Session name of each server instance will be ‘MINECRAFT_SESSION_NAME-INSTANCE_NAME’.

/etc/default/minecraft example:

MINECRAFT_HOME=/export/home/minecraft
MINECRAFT_SERVERS="1.7-server 1.8-server"
MINECRAFT_USE_TMUX=YES

The user (minecraft by default) must have a sh(1) compatible shell in order to run shell commands with the default shell.

The startup script (start.sh by default) must write the PID of server program to server.pid, and must not fork the server program into background.

start.sh example:

#!/bin/sh
JAVA_HOME=/opt/jdk1.7.0_80
echo $$ > server.pid
exec "$JAVA_HOME/bin/java" -jar /opt/minecraft/minecraft-server-1.7.10.jar --nogui

Dependency

  • sudo(1)
  • tmux(1) if enabled by MINECRAFT_USE_TMUX

Service control script for BSD init: rc.d/minecraft

This is very similar with init.d/minecraft, except the configuration variables are in lower case and set from rc.conf instead of /etc/default/minecraft

Example configuration in rc.conf:

minecraft_enable="YES"
minecraft_home="/usr/home/minecraft"
minecraft_use_tmux="YES"
minecraft_servers="1.7-server 1.8-server"

The user and startup script requirements are same with init.d/minecraft.

Dependency

  • tmux(1) if enabled by using minecraft_use_tmux="YES"

Service manifest and control script for SMF: svc/*

svccfg(1M) should be used instead of editing configuration file; configuration properties and its default values are:

$ svccfg -s site/minecraft listprop config
config                application
config/alt_log_file   astring  /dev/null
config/session_name   astring  minecraft
config/start_command  astring  "exec sh start.sh"
config/use_tmux       boolean  false
config/user           astring  minecraft
config/home           astring  /var/games/minecraft
config/servers        astring  

Example configuration:

# svccfg -s site/minecraft setprop config/home = astring: /export/home/minecraft
# svccfg -s site/minecraft setprop config/use_tmux = boolean: true
# svccfg -s site/minecraft setprop config/servers = astring: '("1.7-server" "1.8-server")'
# svcadm enable site/minecraft

Systemd services: systemd/system/*.service

Due to some limitations of systemd, many configuration variables used in previous scripts are hardcoded; service user will always be minecraft; home directory is the home directory of user minecraft; start up script is start.sh under instance directory. Two types of services are available: minecraft@.service and minecraft-tmux@.service; use only one type per instance. Like init.d/minecraft, instances are directories under home directory. Referencing an instance in systemd with minecraft@<instance-name>.service or minecraft-tmux@<instance-name>.service. To configure multiple server instances, just enable every instance via systemctl(1). Unlink other service scripts, writing PID of server program to server.pid is not required in start.sh.

Example configuration:

# systemctl enable --now minecraft-tmux@1.6-server minecraft@1.7-server minecraft@1.8-server

minecraft-tmux@.service dependency

  • tmux(1)

Notice for Bukkit based servers

By default the Bukkit based servers will ignore EOF from stdin, resulting in busy loop if stdin is redirected to /dev/null. The recommended solution is to use tmux(1) with it; however there is also an option --noconsole available, to disable reading from stdin in Bukkit server. For example the start.sh could be written as:

#!/bin/sh
JAVA_HOME=/opt/jdk1.7.0_80
echo $$ > server.pid
tty -s && options= || options="--noconsole --nojline"
exec "$JAVA_HOME/bin/java" -jar /opt/minecraft/craftbukkit-1.2.5-R5.0.jar $options "$@"

This script work with or without tmux(1). Option --nojline disables the line editing library jline, since it would be useless after stdin redirected to /dev/null.