Last active
August 11, 2017 01:32
-
-
Save SilverCory/6a9d1eafa0c7d762814423abab114557 to your computer and use it in GitHub Desktop.
A tmux chrootable service script for RTK.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# /etc/init.d/redstone | |
# version 0.11 2016-03-27 (YYYY-MM-DD) | |
# | |
### BEGIN INIT INFO | |
# Provides: minecraft_redstone | |
# Required-Start: $local_fs $remote_fs screen-cleanup | |
# Required-Stop: $local_fs $remote_fs | |
# Should-Start: $network | |
# Should-Stop: $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Redstone Minecraft server | |
# Description: Starts the redstone.space minecraft server | |
### END INIT INFO | |
###Settings | |
# Normal stuffs | |
SERVICE='redstone' | |
USERNAME='redstone' | |
MCPATH='/home/redstone/' | |
JAIL_PATH="/home/jail/" | |
INVOCATION='./start.sh' | |
#PID_FILE="~/.redstone.pid" | |
# tmux stuffs | |
SESSION_FILE='.tmux_session' | |
SESSION='redstone' | |
WINDOW='server' | |
PANE='0' | |
SESSION_WINDOW_PANE="$SESSION:$WINDOW.$PANE" | |
################################################################################# | |
ME=$(whoami) | |
if [ "$ME" = "$USERNAME" ]; then JAIL_PATH=""; fi | |
as_user() { | |
if [ "$ME" = "$USERNAME" ] ; then | |
bash -c "$1" | |
else | |
if [ ! -z $JAIL_PATH ]; then | |
chroot "$JAIL_PATH" su - "$USERNAME" -c "$1" | |
else | |
su - "$USERNAME" -c "$1" | |
fi | |
fi | |
} | |
as_user "if [ ! -f '$MCPATH/$SESSION_FILE' ]; then touch '$MCPATH/$SESSION_FILE'; chmod 777 '$MCPATH/$SESSION_FILE'; fi" | |
SESSION_FILE=$( as_user "realpath '$MCPATH/$SESSION_FILE'" ) | |
is_server() { | |
return $( as_user "tmux -S '$SESSION_FILE' has-session -t '$SESSION_WINDOW_PANE' &> /dev/null" ) | |
} | |
ssh_command() { | |
ssh -i "$(realpath $1)" -t "[email protected]" "$2" | |
} | |
mc_start() { | |
if is_server; then | |
echo "$SERVICE is already running!" | |
else | |
echo "Starting $SERVICE..." | |
executable_command="tmux -S '$SESSION_FILE' new-session -d -s '$SESSION' -n '$WINDOW' '$INVOCATION; tmux -S $SESSION_FILE kill-session -t $SESSION'" | |
if [ "$ME" = "$ME" ]; then | |
as_user "$executable_command" | |
else | |
if [ -f "$JAIL_PATH/$MCPATH/.ssh/private/internal_rsa" ]; then | |
echo "Using SSH to start server!" | |
ssh_command "$( printf '%s/%s/.ssh/private/internal_rsa' \"$JAIL_PATH\" \"$MCPATH\" )" "$executable_command" | |
else | |
as_user "$executable_command" | |
fi | |
fi | |
sleep 2 | |
if is_server; then | |
echo "$SERVICE is now running." | |
else | |
echo "Error! Could not start $SERVICE!" | |
fi | |
fi | |
} | |
mc_stop() { | |
if is_server; then | |
echo "Stopping $SERVICE" | |
as_user "tmux -S '$SESSION_FILE' send-keys -t \"$SESSION_WINDOW_PANE\" C-z '.stopwrapper' Enter" | |
sleep 15 | |
else | |
echo "$SERVICE was not running." | |
fi | |
if is_server; then | |
echo "Error! $SERVICE could not be stopped." | |
echo " Man, try force-stop instead?" | |
else | |
echo "$SERVICE is stopped." | |
fi | |
} | |
mc_force_stop() { | |
if is_server; then | |
echo "Stopping $SERVICE" | |
as_user "tmux -S '$SESSION_FILE' send-keys -t \"$SESSION_WINDOW_PANE\" C-z '.forcestopwrapper' Enter" | |
sleep 7 | |
else | |
echo "$SERVICE was not running." | |
fi | |
if is_server; then | |
echo "Error! $SERVICE could not be stopped." | |
echo "Sorry, you're going to have to do this one alone!" | |
else | |
echo "$SERVICE is stopped." | |
fi | |
} | |
mc_restart() { | |
if is_server; then | |
echo "Restarting $SERVICE" | |
as_user "tmux -S '$SESSION_FILE' send-keys -t \"$SESSION_WINDOW_PANE\" C-z '.restart' Enter" | |
sleep 7 | |
echo "$SERVICE was restarted." | |
else | |
echo "$SERVICE was not running... Starting!" | |
mc_start | |
fi | |
} | |
mc_command() { | |
COMMAND="$*" | |
pre_log_len=$(wc -l "$JAIL_PATH/$MCPATH/server/logs/latest.log" | awk '{print $1}') | |
echo "$SERVICE is running... executing command" | |
as_user "tmux -S '$SESSION_FILE' send-keys -t \"$SESSION_WINDOW_PANE\" C-z '$COMMAND' Enter" | |
sleep .8 # assumes that the command will run and print to the log file in less than .8 seconds | |
# print output | |
tail -n "$(wc -l \"$JAIL_PATH/$MCPATH/server/logs/latest.log\" | awk '{print $1}')"-"$pre_log_len" "$JAIL_PATH/$MCPATH/server/logs/latest.log" | |
} | |
#Start-Stop here | |
case "$1" in | |
start) | |
mc_start | |
;; | |
stop) | |
mc_stop | |
;; | |
force_stop) | |
mc_force_stop | |
;; | |
restart) | |
mc_restart | |
;; | |
status) | |
if is_server; then | |
echo "$SERVICE is running." | |
else | |
echo "$SERVICE is not running." | |
fi | |
;; | |
att) | |
tmux -S "$JAIL_PATH/$SESSION_FILE" att -t "$SESSION" | |
;; | |
command) | |
if [ $# -gt 1 ] ; then | |
shift | |
if ! is_server; then | |
echo "$SERVICE isn't running.." | |
exit 1 | |
else | |
mc_command "$*" | |
fi | |
else | |
echo "Must specify server command (try 'help'?)" | |
fi | |
;; | |
*) | |
echo "Usage: $0 {start|stop|force_stop|status|restart|command \"server command\"}" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment