aboutsummarylogtreecommitdiffstats
path: root/papermc.sh
diff options
context:
space:
mode:
authorGordian Edenhofer2021-07-13 09:42:29 +0200
committerGordian Edenhofer2021-07-13 09:50:11 +0200
commit1732e8c68c66b860493c9efc1b309a72eb7305a6 (patch)
treeb318fddde00f9b581b10c975e36a83a5e6bd38b3 /papermc.sh
parentb6e85ebbf7e76abe8aa1db7cbc280e8a1be6e22d (diff)
downloadaur-1732e8c68c66b860493c9efc1b309a72eb7305a6.tar.gz
upgpkg: minecraft-server 1.17.1-2
Sync the minecraft-server management script across different minecraft-server flavors. Notify the user of the switch to tmux during post-install with a consistent message. upgpkg: cuberite 1.12.2c0_094cd61-2 upgpkg: cuberite-bin 1.12.2b258-2 upgpkg: papermc 1.17.1+b100-2 upgpkg: spigot 1.17.1+b131-2
Diffstat (limited to 'papermc.sh')
-rwxr-xr-xpapermc.sh97
1 files changed, 55 insertions, 42 deletions
diff --git a/papermc.sh b/papermc.sh
index e4e7ed485025..cfc90446ac2d 100755
--- a/papermc.sh
+++ b/papermc.sh
@@ -68,22 +68,23 @@ fi
# Pipe any given argument to the game server console,
# sleep for $sleep_time and return its output if $return_stdout is set
game_command() {
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" wait-for -L "command_lock"
if [[ -z "${return_stdout:-}" ]]; then
- ${SUDO_CMD} screen -S "${SESSION_NAME}" -X stuff "$(printf "%s\r" "$*")"
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" send-keys -t "${SESSION_NAME}":0.0 "$*" Enter
else
- ${SUDO_CMD} screen -S "${SESSION_NAME}" -X log on
- ${SUDO_CMD} screen -S "${SESSION_NAME}" -X stuff "$(printf "%s\r" "$*")"
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" pipe-pane -t "${SESSION_NAME}":0.0 "cat > ${GAME_COMMAND_DUMP}"
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" send-keys -t "${SESSION_NAME}":0.0 "$*" Enter
sleep "${sleep_time:-0.3}"
- ${SUDO_CMD} screen -S "${SESSION_NAME}" -X log off
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" pipe-pane -t "${SESSION_NAME}":0.0
${SUDO_CMD} cat "${GAME_COMMAND_DUMP}"
- ${SUDO_CMD} rm "${GAME_COMMAND_DUMP}"
fi
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" wait-for -U "command_lock"
}
# Check whether there are player on the server through list
is_player_online() {
response="$(sleep_time=0.6 return_stdout=true game_command list)"
- # Delete leading line and free response string from fancy characters
+ # Delete leading line and fancy characters from free response string
response="$(echo "${response}" | sed -r -e 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})*)?[JKmsuG]//g')"
# The list command prints a line containing the usernames after the last occurrence of ": "
# and since playernames may not contain this string the clean player-list can easily be retrieved.
@@ -103,7 +104,7 @@ is_player_online() {
# Check whether the server is visited by a player otherwise shut it down
idle_server_daemon() {
- # This function is run within a screen session of the GAME_USER therefore SUDO_CMD can be omitted
+ # This function is run within a tmux session of the GAME_USER therefore SUDO_CMD can be omitted
if [[ "$(whoami)" != "${GAME_USER}" ]]; then
>&2 echo "Somehow this hidden function was not executed by the ${GAME_USER} user."
>&2 echo "This should not have happend. Are you messing around with this script? :P"
@@ -114,17 +115,14 @@ idle_server_daemon() {
no_player=0
while true; do
- echo -e "no_player: ${no_player}s\tcheck_player_time: ${CHECK_PLAYER_TIME}s\tidle_if_time: ${IDLE_IF_TIME}s"
+ printf "no_player: %10ss check_player_time: %10ss idle_if_time: %10ss\n" "${no_player}" "${CHECK_PLAYER_TIME}" "${IDLE_IF_TIME}"
# Retry in ${CHECK_PLAYER_TIME} seconds
sleep ${CHECK_PLAYER_TIME}
- if screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
+ if socket_has_session "${SESSION_NAME}"; then
# Game server is up and running
- if [[ "$(screen -S "${SESSION_NAME}" -ls | sed -n "s/.*${SESSION_NAME}\s\+//gp")" == "(Attached)" ]]; then
- # An administrator is connected to the console, pause player checking
- echo "An admin is connected to the console. Pause player checking."
# Check for active player
- elif SUDO_CMD="" is_player_online; then
+ if SUDO_CMD="" is_player_online; then
# No player was seen on the server through list
no_player=$(( no_player + CHECK_PLAYER_TIME ))
# Stop the game server if no player was active for at least ${IDLE_IF_TIME}
@@ -132,7 +130,7 @@ idle_server_daemon() {
IDLE_SERVER="false" ${myname} stop
# Wait for game server to go down
for i in {1..100}; do
- screen -S "${SESSION_NAME}" -Q select . > /dev/null || break
+ socket_has_session "${SESSION_NAME}" || break
[[ $i -eq 100 ]] && echo -e "An \e[39;1merror\e[0m occurred while trying to reset the idle_server!"
sleep 0.1
done
@@ -163,12 +161,16 @@ idle_server_daemon() {
# Start the server if it is not already running
server_start() {
# Start the game server
- if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
- echo "A screen ${SESSION_NAME} session is already running. Please close it first."
+ if socket_has_session "${SESSION_NAME}"; then
+ echo "A tmux ${SESSION_NAME} session is already running. Please close it first."
else
echo -en "Starting server..."
- ${SUDO_CMD} screen -dmS "${SESSION_NAME}" /bin/bash -c "cd '${SERVER_ROOT}'; ${SERVER_START_CMD}"
- ${SUDO_CMD} screen -S "${SESSION_NAME}" -X logfile "${GAME_COMMAND_DUMP}"
+ ${SUDO_CMD} rm -f "${GAME_COMMAND_DUMP}"
+ ${SUDO_CMD} mkfifo "${GAME_COMMAND_DUMP}"
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" new-session -s "${SESSION_NAME}" -c "${SERVER_ROOT}" -d "${SERVER_START_CMD}"
+
+ # Mimic GNU screen and allow for both C-a and C-b as prefix
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" set -g prefix2 C-a
echo -e "\e[39;1m done\e[0m"
fi
@@ -180,12 +182,12 @@ server_start() {
fi
# Start the idle server daemon
- if ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null; then
- ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -X quit
+ if socket_has_session "${IDLE_SESSION_NAME}"; then
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" kill-session -t "${IDLE_SESSION_NAME}"
# Restart as soon as the idle_server_daemon has shut down completely
for i in {1..100}; do
- if ! ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null; then
- ${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${myname} idle_server_daemon"
+ if ! socket_has_session "${IDLE_SESSION_NAME}"; then
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" new-session -s "${IDLE_SESSION_NAME}" -d "${myname} idle_server_daemon"
break
fi
[[ $i -eq 100 ]] && echo -e "An \e[39;1merror\e[0m occurred while trying to reset the idle_server!"
@@ -193,7 +195,7 @@ server_start() {
done
else
echo -en "Starting idle server daemon..."
- ${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${myname} idle_server_daemon"
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" new-session -s "${IDLE_SESSION_NAME}" -d "${myname} idle_server_daemon"
echo -e "\e[39;1m done\e[0m"
fi
fi
@@ -209,17 +211,17 @@ server_stop() {
exit 12
fi
- if ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null; then
+ if socket_has_session "${IDLE_SESSION_NAME}"; then
echo -en "Stopping idle server daemon..."
- ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -X quit
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" kill-session -t "${IDLE_SESSION_NAME}":0.0
echo -e "\e[39;1m done\e[0m"
else
- echo "The corresponding screen session for ${IDLE_SESSION_NAME} was already dead."
+ echo "The corresponding tmux session for ${IDLE_SESSION_NAME} was already dead."
fi
fi
# Gracefully exit the game server
- if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
+ if socket_has_session "${SESSION_NAME}"; then
# Game server is up and running, gracefully stop the server when there are still active players
# Check for active player
@@ -243,7 +245,8 @@ server_stop() {
# Finish as soon as the server has shut down completely
for i in {1..100}; do
- if ! ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
+ if ! socket_has_session "${SESSION_NAME}"; then
+ ${SUDO_CMD} rm -f "${GAME_COMMAND_DUMP}"
echo -e "\e[39;1m done\e[0m"
break
fi
@@ -251,7 +254,7 @@ server_stop() {
sleep 0.1
done
else
- echo "The corresponding screen session for ${SESSION_NAME} was already dead."
+ echo "The corresponding tmux session for ${SESSION_NAME} was already dead."
fi
}
@@ -265,7 +268,7 @@ server_status() {
exit 12
fi
- if ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null; then
+ if socket_has_session "${IDLE_SESSION_NAME}"; then
echo -e "Idle server daemon status:\e[39;1m running\e[0m"
else
echo -e "Idle server daemon status:\e[39;1m stopped\e[0m"
@@ -273,13 +276,13 @@ server_status() {
fi
# Print status information for the game server
- if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
+ if socket_has_session "${SESSION_NAME}"; then
echo -e "Status:\e[39;1m running\e[0m"
# Calculating memory usage
for p in $(${SUDO_CMD} pgrep -f "${MAIN_EXECUTABLE}"); do
ps -p"${p}" -O rss | tail -n 1;
- done | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =", count, "(screen, bash,", count-2, "x server)"; print "Total memory usage =", sum/1024, "MB" ;};'
+ done | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =", count, "(tmux,", count-1, "x server)"; print "Total memory usage =", sum/1024, "MB" ;};'
else
echo -e "Status:\e[39;1m stopped\e[0m"
fi
@@ -287,7 +290,7 @@ server_status() {
# Restart the complete server by shutting it down and starting it again
server_restart() {
- if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
+ if socket_has_session "${SESSION_NAME}"; then
server_stop
server_start
else
@@ -306,7 +309,7 @@ backup_files() {
echo "Starting backup..."
fname="$(date +%Y_%m_%d_%H.%M.%S).tar.gz"
${SUDO_CMD} mkdir -p "${BACKUP_DEST}"
- if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
+ if socket_has_session "${SESSION_NAME}"; then
game_command save-off
game_command save-all
sync && wait
@@ -338,7 +341,7 @@ backup_restore() {
fi
# Only allow the user to restore a backup if the server is down
- if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
+ if socket_has_session "${SESSION_NAME}"; then
>&2 echo -e "The \e[39;1mserver should be down\e[0m in order to restore the world data."
exit 3
fi
@@ -404,24 +407,34 @@ server_command() {
exit 1
fi
- if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
+ if socket_has_session "${SESSION_NAME}"; then
return_stdout=true game_command "$@"
else
echo "There is no ${SESSION_NAME} session to connect to."
fi
}
-# Enter the screen game session
+# Enter the tmux game session
server_console() {
- if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then
- # Circumvent a permission bug related to running GNU screen as a different user,
- # see e.g. https://serverfault.com/questions/116775/sudo-as-different-user-and-running-screen
- ${SUDO_CMD} script -q -c "screen -S \"${SESSION_NAME}\" -rx" /dev/null
+ if socket_has_session "${SESSION_NAME}"; then
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" wait-for -L "command_lock"
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" attach -t "${SESSION_NAME}":0.0
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" wait-for -U "command_lock"
else
echo "There is no ${SESSION_NAME} session to connect to."
fi
}
+# Check if there is a session available
+socket_has_session() {
+ if [[ "$(whoami)" != "${GAME_USER}" ]]; then
+ ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t "${1}":0.0 2> /dev/null
+ return $?
+ fi
+ tmux -L "${SESSION_NAME}" has-session -t "${1}":0.0 2> /dev/null
+ return $?
+}
+
# Help function, no arguments required
help() {
cat <<-EOF
@@ -436,7 +449,7 @@ help() {
backup Backup the world data
restore [filename] Restore the world data from a backup
command <command> Run the given command at the ${game} server console
- console Enter the server console through a screen session
+ console Enter the server console through a tmux session
Copyright (c) Gordian Edenhofer <gordian.edenhofer@gmail.com>
EOF