diff options
author | StaticRocket | 2021-06-27 18:41:07 -0400 |
---|---|---|
committer | Gordian Edenhofer | 2021-07-13 09:33:55 +0200 |
commit | 1a75ddf0d145e8f0ac9c3b078f6396d07ca5f819 (patch) | |
tree | 883a2c5cae5bfc03e8e771115a215b11b8e4b7e5 | |
parent | 8242e6b59dfca11ba20731b69c6d9e8e2be68b9d (diff) | |
download | aur-1a75ddf0d145e8f0ac9c3b078f6396d07ca5f819.tar.gz |
minecraft-server: Check session and attachment
-rw-r--r-- | .SRCINFO | 4 | ||||
-rw-r--r-- | PKGBUILD | 4 | ||||
-rwxr-xr-x | minecraftd.sh | 43 |
3 files changed, 26 insertions, 25 deletions
@@ -6,7 +6,7 @@ pkgbase = minecraft-server arch = any license = custom depends = java-runtime-headless>=16 - depends = screen + depends = tmux depends = sudo depends = bash depends = awk @@ -32,6 +32,6 @@ pkgbase = minecraft-server sha512sums = a62c8c04e08dbac0db0aa2eeb505d70f8fd925bd2e427899512ba3ac828d4644e1c43c8d92325c6bc49c8d9ecb40cb5c44bf5957a63980b1e2cf86fdb38a05a7 sha512sums = a74f4e31065b6c6f5c830182de05a8c75a6de6eaac7b3dc26479827646ef20ab872509aac88be613048c97d378711c38612ec7ac92d22134acefd40f6e0a99da sha512sums = 30e434ba183527da8047b1ffe403a083f1af34dbd229b5871222e9da0004cdf5d4152eaa4b73215befcb1233d08cc757af32ad6b572f4b6d2a623b6f120aa0d9 - sha512sums = 37fef61051416d9313e13c75ace5235f110b2a38207a30743427c5337c18d198ac2732795229a7fb3d81e88fabfecd534aa91cf873767c279acc41459599b913 + sha512sums = e6ffe546ce05ad78c29601977de35cd0889e95aa821ee7c6051b8ec97369ac4438ded441bd1768eeef5cf1500ff3c6419115d306d62acdebecb33a86402eb5c1 pkgname = minecraft-server @@ -10,7 +10,7 @@ pkgdesc="Minecraft server unit files, script, and jar" arch=('any') url="https://minecraft.net/" license=('custom') -depends=('java-runtime-headless>=16' 'screen' 'sudo' 'bash' 'awk' 'sed') +depends=('java-runtime-headless>=16' 'tmux' 'sudo' 'bash' 'awk' 'sed') optdepends=("tar: needed in order to create world backups" "netcat: required in order to suspend an idle server") conflicts=('minecraft-server-systemd' 'minecraft-canary') @@ -32,7 +32,7 @@ sha512sums=('fe6e48e2cee38224d2c88f04c19afca9c21fddbe6077b5538e0a0581c2f6c4478ec 'a62c8c04e08dbac0db0aa2eeb505d70f8fd925bd2e427899512ba3ac828d4644e1c43c8d92325c6bc49c8d9ecb40cb5c44bf5957a63980b1e2cf86fdb38a05a7' 'a74f4e31065b6c6f5c830182de05a8c75a6de6eaac7b3dc26479827646ef20ab872509aac88be613048c97d378711c38612ec7ac92d22134acefd40f6e0a99da' '30e434ba183527da8047b1ffe403a083f1af34dbd229b5871222e9da0004cdf5d4152eaa4b73215befcb1233d08cc757af32ad6b572f4b6d2a623b6f120aa0d9' - '37fef61051416d9313e13c75ace5235f110b2a38207a30743427c5337c18d198ac2732795229a7fb3d81e88fabfecd534aa91cf873767c279acc41459599b913') + 'e6ffe546ce05ad78c29601977de35cd0889e95aa821ee7c6051b8ec97369ac4438ded441bd1768eeef5cf1500ff3c6419115d306d62acdebecb33a86402eb5c1') _game="minecraft" _server_root="/srv/minecraft" diff --git a/minecraftd.sh b/minecraftd.sh index cf5aa623180d..b7630a71a234 100755 --- a/minecraftd.sh +++ b/minecraftd.sh @@ -71,7 +71,7 @@ game_command() { if [[ -z "${return_stdout:-}" ]]; then ${SUDO_CMD} tmux -L "${SESSION_NAME}" send-keys -t0:0 "$(printf "%s" "$*")" Enter else - ${SUDO_CMD} tmux -L "${SESSION_NAME}" pipe-pane -t0:0 -o "cat > ${GAME_COMMAND_DUMP}" + ${SUDO_CMD} tmux -L "${SESSION_NAME}" pipe-pane -t0:0 "cat > ${GAME_COMMAND_DUMP}" ${SUDO_CMD} tmux -L "${SESSION_NAME}" send-keys -t0:0 "$(printf "%s" "$*")" Enter sleep "${sleep_time:-0.3}" ${SUDO_CMD} tmux -L "${SESSION_NAME}" pipe-pane -t0:0 @@ -91,7 +91,7 @@ is_player_online() { if [[ $(echo "${response}" | grep ":" | sed -e 's/.*\: //' | tr -d '\n' | wc -c) -le 1 ]]; then # No player is online return 0 - elif [[ "x$(echo "${response}" | grep "There are" | sed -r -e 's/.*\: //' -e 's/^([^.]+).*$/\1/; s/^[^0-9]*([0-9]+).*$/\1/' | tr -d '\n')" == "x0" ]]; then + elif [[ "$(echo "${response}" | grep "There are" | sed -r -e 's/.*\: //' -e 's/^([^.]+).*$/\1/; s/^[^0-9]*([0-9]+).*$/\1/' | tr -d '\n')" == "x0" ]]; then # No player is online return 0 else @@ -117,9 +117,9 @@ idle_server_daemon() { # Retry in ${CHECK_PLAYER_TIME} seconds sleep ${CHECK_PLAYER_TIME} - if tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if tmux -L "${SESSION_NAME}" has-session -t0:0; then # Game server is up and running - if [[ "$(tmux -L "${SESSION_NAME}" -ls | sed -n "s/.*${SESSION_NAME}\s\+//gp")" == "(Attached)" ]]; then + if [ -n "$(tmux list-clients -t0:0)" ]; 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 @@ -131,7 +131,7 @@ idle_server_daemon() { IDLE_SERVER="false" ${myname} stop # Wait for game server to go down for i in {1..100}; do - tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null || break + tmux -L "${SESSION_NAME}" has-session -t0:0 || break [[ $i -eq 100 ]] && echo -e "An \e[39;1merror\e[0m occurred while trying to reset the idle_server!" sleep 0.1 done @@ -139,7 +139,7 @@ idle_server_daemon() { no_player=$(( IDLE_IF_TIME - 300 )) # Game server is down, listen on port ${GAME_PORT} for incoming connections echo -n "Netcat: " - ${NETCAT_CMD} -v -l -p ${GAME_PORT} 2>&1 | (grep -m1 -i "connect" && pkill -P $$ ${NETCAT_CMD}) || true + "${NETCAT_CMD}" -v -l -p ${GAME_PORT} 2>&1 | (grep -m1 -i "connect" && pkill -P $$ "${NETCAT_CMD}") || true echo "Netcat caught a connection. The server is coming up again..." IDLE_SERVER="false" ${myname} start fi @@ -152,7 +152,7 @@ idle_server_daemon() { no_player=$(( IDLE_IF_TIME - 300 )) # Game server is down, listen on port ${GAME_PORT} for incoming connections echo -n "Netcat: " - ${NETCAT_CMD} -v -l -p ${GAME_PORT} 2>&1 | (grep -m1 -i "connect" && pkill -P $$ ${NETCAT_CMD}) || true + ${NETCAT_CMD} -v -l -p ${GAME_PORT} 2>&1 | (grep -m1 -i "connect" && pkill -P $$ "${NETCAT_CMD}") || true echo "Netcat caught a connection. The server is coming up again..." IDLE_SERVER="false" ${myname} start fi @@ -162,12 +162,13 @@ idle_server_daemon() { # Start the server if it is not already running server_start() { # Start the game server - if ${SUDO_CMD} tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t0:0; then echo "A tmux ${SESSION_NAME} session is already running. Please close it first." else echo -en "Starting server..." + ${SUDO_CMD} rm -f "${GAME_COMMAND_DUMP}" ${SUDO_CMD} mkfifo "${GAME_COMMAND_DUMP}" - ${SUDO_CMD} tmux -L "${SESSION_NAME}" new-session -d "cd ${SERVER_ROOT} && ${SERVER_START_CMD}" + ${SUDO_CMD} tmux -L "${SESSION_NAME}" new-session -c "${SERVER_ROOT}" -d "${SERVER_START_CMD}" echo -e "\e[39;1m done\e[0m" fi @@ -179,11 +180,11 @@ server_start() { fi # Start the idle server daemon - if ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" has-session -t0:0; then ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" send-keys -t0:0 "quit" Enter # Restart as soon as the idle_server_daemon has shut down completely for i in {1..100}; do - if ! ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ! ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" has-session -t0:0; then ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" new-session -d "${myname} idle_server_daemon" break fi @@ -208,7 +209,7 @@ server_stop() { exit 12 fi - if ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" has-session -t0:0; then echo -en "Stopping idle server daemon..." ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" send-keys -t0:0 "quit" Enter echo -e "\e[39;1m done\e[0m" @@ -218,7 +219,7 @@ server_stop() { fi # Gracefully exit the game server - if ${SUDO_CMD} tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t0:0; then # Game server is up and running, gracefully stop the server when there are still active players # Check for active player @@ -242,7 +243,7 @@ server_stop() { # Finish as soon as the server has shut down completely for i in {1..100}; do - if ! ${SUDO_CMD} tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ! ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t0:0; then ${SUDO_CMD} rm ${GAME_COMMAND_DUMP} echo -e "\e[39;1m done\e[0m" break @@ -265,7 +266,7 @@ server_status() { exit 12 fi - if ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${IDLE_SESSION_NAME}" has-session -t0:0; 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,7 +274,7 @@ server_status() { fi # Print status information for the game server - if ${SUDO_CMD} tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t0:0; then echo -e "Status:\e[39;1m running\e[0m" # Calculating memory usage @@ -287,7 +288,7 @@ server_status() { # Restart the complete server by shutting it down and starting it again server_restart() { - if ${SUDO_CMD} tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t0:0; then server_stop server_start else @@ -306,7 +307,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} tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t0:0; then game_command save-off game_command save-all sync && wait @@ -338,7 +339,7 @@ backup_restore() { fi # Only allow the user to restore a backup if the server is down - if ${SUDO_CMD} tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t0:0; then >&2 echo -e "The \e[39;1mserver should be down\e[0m in order to restore the world data." exit 3 fi @@ -404,7 +405,7 @@ server_command() { exit 1 fi - if ${SUDO_CMD} tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t0:0; then return_stdout=true game_command "$@" else echo "There is no ${SESSION_NAME} session to connect to." @@ -413,7 +414,7 @@ server_command() { # Enter the tmux game session server_console() { - if ${SUDO_CMD} tmux -L "${SESSION_NAME}" select-window -t0:0 2> /dev/null; then + if ${SUDO_CMD} tmux -L "${SESSION_NAME}" has-session -t0:0; then ${SUDO_CMD} tmux -L "${SESSION_NAME}" attach -t0:0 else echo "There is no ${SESSION_NAME} session to connect to." |