#!/bin/bash # vim: set filetype=sh: # # Necessary environment variables: # INITD=true, NAME, ACTION, PIDFILE, SERVER_BASENAME, SERVER_JAR, SERVER_PREPARE_STOP_CMD # # Optional environment variables: # SERVER_OPTS, SERVER_JAVA_OPTS, SERVER_PREPARE_OPTS, SERVER_PREPARE_JAVA_OPTS, SERVER_BLOCK_BEFORE_START einfo() { echo " * $*" } eerror() { echo " * ERROR: $*" >&2 } die() { eerror "$*" exit 1 } # Invocation protection [[ $INITD == true ]] \ || die "Do not call $SERVER_BASENAME directly! Use /etc/init.d/$SERVER_BASENAME" # Find JAVA_HOME export JAVA_HOME="$(java-config --jre-home)" # Disable spigot outdated messages export IReallyKnowWhatIAmDoingISwear=1 exec_fork() { setsid "$@" & } MAX_SECONDS_WAIT=300 wait_for_pidfile_to_disappear() { # Wait for pidfile to disappear local seconds_waited=0 while [[ $seconds_waited -lt $MAX_SECONDS_WAIT ]] ; do [[ -e $PIDFILE ]] || break sleep 1 ((++seconds_waited)) done [[ $seconds_waited -lt $MAX_SECONDS_WAIT ]] } # Prepare TMUX command TMUX_SOCKET="/var/lib/$SERVER_BASENAME/tmux/$NAME" TMUX_EXEC="/usr/bin/tmux -2 -f /etc/tmux.conf -S $TMUX_SOCKET set -g default-shell /bin/bash ; " BLOCK_FILE="/var/run/$SERVER_BASENAME/block/$NAME" case "$ACTION" in "--prepare") # Preparing means starting the server once, then stopping it immediately. [[ ! -e sublime-local.conf ]] && install --mode=600 --owner=sublime /usr/share/sublime/sublime-local.conf . echo -e "$SERVER_PREPARE_STOP_CMD" | java $SERVER_PREPARE_JAVA_OPTS -jar $SERVER_JAR $SERVER_PREPARE_OPTS \ || die "Server returned unsuccessful status while preparing files" exit 0 ;; "--attach") $TMUX_EXEC attach-session ;; "--stop") einfo "Please be patient, stopping the server can take some time (up to $MAX_SECONDS_WAIT seconds)." # Kill the tmux server $TMUX_EXEC kill-server 2>/dev/null wait_for_pidfile_to_disappear \ || eerror "Pidfile still existent after $MAX_SECONDS_WAIT seconds. Forcefully stopping the server." true ;; "--start") $TMUX_EXEC kill-server 2>/dev/null wait_for_pidfile_to_disappear if [[ $SERVER_BLOCK_BEFORE_START == yes ]] ; then exec_fork $TMUX_EXEC new-session -d \ loop --pidfile "$PIDFILE" \ chain --delimiter='&&' \ block -v --file "$BLOCK_FILE" '&&' \ java $SERVER_JAVA_OPTS -jar $SERVER_JAR $SERVER_OPTS else exec_fork $TMUX_EXEC new-session -d \ loop --pidfile "$PIDFILE" \ java $SERVER_JAVA_OPTS -jar $SERVER_JAR $SERVER_OPTS fi ;; *) die "Invalid usage" ;; esac