Skip to content

Instantly share code, notes, and snippets.

@rmarmorstein
Forked from literalplus/00_Spigotctl
Created March 15, 2017 13:33
Show Gist options
  • Save rmarmorstein/4c773faeb946e72d65d74d37cd705370 to your computer and use it in GitHub Desktop.
Save rmarmorstein/4c773faeb946e72d65d74d37cd705370 to your computer and use it in GitHub Desktop.
Spigotctl, control 'framework' for Spigot servers running in RemoteToolkit in Tmux.
MIT License
Copyright (c) 2016 Philipp Nowak
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/bin/bash
# /usr/bin/spigotctl
# Systemd service backend for Spigot servers running in tmux
# Copyright (C) 2016 Philipp Nowak, licensed under the MIT License.
## NOTE: This *can* be used to execute arbitrary commands, but
## then again, if somebody has permission to use sudo, they
## can do that anyways.
######## CONFIGURATION SECTION
# what user to run as
TMUX_USER="minecraft"
######## CONFIGURATION END
SERVER_NAME="$2"
SERVER_DIR="/home/minecraft/$SERVER_NAME"
if [ $(whoami) != "$TMUX_USER" ]; then
echo "$0: must run as $TMUX_USER (is: $(whoami))"
exit 1
fi
tmux ls >/dev/null
if [ "$?" -ne 0 ]; then
echo "$0: tmux server must be running!"
exit 1
fi
function check_name {
if [ -z "$SERVER_NAME" ]; then
echo "$0: need a server name!"
exit 1
fi
if [ ! -d "$SERVER_DIR" ]; then
echo "$0: directory $SERVER_DIR must exist!"
exit 1
fi
check_multiple_windows
calc_num_panes
}
function calc_num_windows {
NUM_MATCHES="$(tmux list-windows | grep $SERVER_NAME | wc -l)"
}
function check_multiple_windows {
calc_num_windows
if [ "$NUM_MATCHES" -gt 1 ]; then
echo "$0: multiple windows of this name in existence ($NUM_MATCHES)"
exit 1
fi
}
function check_window_exists {
calc_num_windows
if [ "$NUM_MATCHES" -ne 1 ]; then
echo "$0: need exactly one window of name '$SERVER_NAME', not $NUM_MATCHES"
exit 1
fi
}
function calc_num_panes {
NUMPANES="$(tmux list-windows | grep $SERVER_NAME | sed -r 's/.*([0-9]) panes.*/\1/')"
if [ -z "$NUMPANES" ]; then
echo "$0: info: window does not exist yet ($NUMPANES)"
NUMPANES=0
TARGETPANE=0
return 0
fi
TARGETPANE="$(expr $NUMPANES - 1)"
if [ "$TARGETPANE" -eq -1 ]; then
TARGETPANE=0
fi
}
function respawn {
if [ "$NUMPANES" -eq 0 ]; then
# echo "$0: Spawning new window because only $NUMPANES panes exist"
tmux new-window -n "$SERVER_NAME" -t: 'bash'
fi
tmux select-window -t ":$SERVER_NAME"
tmux respawn-pane -t ":$SERVER_NAME.$TARGETPANE" -k 'bash'
tmux display-message -t ":$SERVER_NAME" "Respawned due to systemctl invocation"
}
function keys {
tmux send-keys -t ":$SERVER_NAME.$TARGETPANE" "$*" Enter
}
function message {
tmux display-message -t ":$SERVER_NAME" "$*"
}
function cmd_exec {
check_name
keys "${@:3}"
}
function cmd_logfile {
check_name
if [ -n "$SCTL_FOLLOW" ]; then
ARGS="+F"
else
ARGS="+G"
fi
LESSSECURE=1 less -r "$ARGS" ~minecraft/"$SERVER_NAME"/logs/latest.log
}
case "$1" in
start)
check_name
respawn
keys "cd $SERVER_DIR"
keys "./rtoolkit.sh"
;;
stop)
check_name
respawn
keys "clear && echo -e \"\e[0;49;91mWrapper is stopped. Restart using 'systemctl start minecraft@%SERVER_NAME'.\e[0m\" && cat"
;;
reload)
check_name
check_window_exists
keys "stop --" # apparently $* needs multiple arguments or something
;;
exec)
cmd_exec $*
;;
lexec)
cmd_exec $*
SCTL_FOLLOW=1 cmd_logfile $1
;;
logfile)
cmd_logfile $*
;;
*)
echo "Usage: "
echo " sctl start <server> - starts a wrapper"
echo " sctl stop <server> - stops a wrapper"
echo " sctl reload <server> - restarts a server (stop command)"
echo " sctl exec <server> <command> - executes a command"
echo " sctl lexec <server> <command> - executes a command and shows output"
echo " sctl logfile <server> - shows the latest server log file"
exit 1
esac
message "$0: $*"
#!/bin/bash
# /usr/bin/sctl
# wraps around spigotctl for easier calling
# Copyright (C) 2016 Philipp Nowak, licensed under the MIT License.
######## CONFIGURATION SECTION
# what user to run as
TMUX_USER="minecraft"
######## CONFIGURATION END
sudo -u "$TMUX_USER" -i spigotctl $*
# /etc/systemd/system/minecraft@.service
# Systemd Unit file for Spigot running in tmux
# Also see /usr/bin/spigotctl
# Copyright (C) 2016 Philipp Nowak, licensed under the MIT License.
[Unit]
Description=Spigot server %i running in tmux
Requires=tmux@minecraft.service
[Service]
Type=oneshot
RemainAfterExit=true
User=minecraft
ExecStart=/usr/bin/spigotctl start %i
ExecStop=/usr/bin/spigotctl stop %i
ExecReload=/usr/bin/spigotctl reload %i
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/tmux@.service
# Systemd unit file for tmux starting in detached sessions
# Copyright (C) 2016 Philipp Nowak, licensed under the MIT License.
[Unit]
Description=Start tmux in detached session
[Service]
Type=forking
User=%I
ExecStart=/usr/bin/tmux -2 -u new-session -d
ExecStop=/usr/bin/tmux kill-session
WorkingDirectory=/home/%I
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment