Parcourir la source

Fix(tmux): better tmux session handling for multiple instance on same server / user session (#3767)

* fix(tmux): force tmux to create a new trmux server instance for each game instance to avoid sharing same pid.

* refactor: improve tmux command consistency and add session name flag

This commit refactors the code to improve consistency in the usage of the `tmux` command. It adds the `-L` flag followed by the session name to all `tmux` commands. This ensures that all commands are executed within the correct session. Additionally, it updates the `check_status.sh`, `command_console.sh`, `command_send.sh`, `command_start.sh`, `command_stop.sh`, and `info_distro.sh` files to include this change.

The changes ensure that all relevant commands are executed within the specified session, improving code readability and maintainability.

---------

Co-authored-by: cclecle <clement.chastanier@gmail.com>
Co-authored-by: Daniel Gibbs <me@danielgibbs.co.uk>
ChaCha il y a 2 ans
Parent
commit
e9f2453e48

+ 1 - 1
lgsm/functions/check_status.sh

@@ -7,4 +7,4 @@
 
 functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
 
-status=$(tmux list-sessions -F "#{session_name}" 2> /dev/null | grep -Ecx "^${sessionname}")
+status=$(tmux -L "${sessionname}" list-sessions -F "#{session_name}" 2>/dev/null | grep -Ecx "^${sessionname}")

+ 1 - 1
lgsm/functions/command_console.sh

@@ -42,7 +42,7 @@ check_status.sh
 if [ "${status}" != "0" ]; then
 	fn_print_ok_nl "Accessing console"
 	fn_script_log_pass "Console accessed"
-	tmux attach-session -t "${sessionname}"
+	tmux -L "${sessionname}" attach-session -t "${sessionname}"
 	fn_print_ok_nl "Closing console"
 	fn_script_log_pass "Console closed"
 else

+ 1 - 1
lgsm/functions/command_send.sh

@@ -26,7 +26,7 @@ if [ "${status}" != "0" ]; then
 	fi
 	echo ""
 	fn_print_dots "Sending command to console: \"${commandtosend}\""
-	tmux send-keys -t "${servicename}" "${commandtosend}" ENTER
+	tmux -L "${sessionname}" send-keys -t "${servicename}" "${commandtosend}" ENTER
 	fn_print_ok_nl "Sending command to console: \"${commandtosend}\""
 	fn_script_log_pass "Command \"${commandtosend}\" sent to console"
 else

+ 5 - 5
lgsm/functions/command_start.sh

@@ -37,7 +37,7 @@ fn_start_teamspeak3() {
 # Used to allow update to detect JK2MV server version.
 fn_start_jk2() {
 	fn_start_tmux
-	tmux send -t "${sessionname}" version ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" send -t "${sessionname}" version ENTER > /dev/null 2>&1
 }
 
 fn_start_tmux() {
@@ -80,7 +80,7 @@ fn_start_tmux() {
 		cd "${executabledir}" || exit
 	fi
 
-	tmux new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
+	tmux -L "${sessionname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
 
 	# Create logfile.
 	touch "${consolelog}"
@@ -94,9 +94,9 @@ fn_start_tmux() {
 		echo -e "tmux version: master (user compiled)" >> "${consolelog}"
 		if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
 			if [ "$logtimestamp" == "on" ]; then
-				tmux pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
+				tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
 			else
-				tmux pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
+				tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
 			fi
 		fi
 
@@ -143,7 +143,7 @@ fn_start_tmux() {
 			echo -e ""
 			echo -e "Command"
 			echo -e "================================="
-			echo -e "tmux new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
+			echo -e "tmux -L \"${sessionname}\" new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
 			echo -e ""
 			echo -e "Error"
 			echo -e "================================="

+ 6 - 6
lgsm/functions/command_stop.sh

@@ -15,7 +15,7 @@ fn_stop_graceful_ctrlc() {
 	fn_print_dots "Graceful: CTRL+c"
 	fn_script_log_info "Graceful: CTRL+c"
 	# Sends quit.
-	tmux send-keys -t "${sessionname}" C-c > /dev/null 2>&1
+	tmux -L "${sessionname}" send-keys -t "${sessionname}" C-c  > /dev/null 2>&1
 	# Waits up to 30 seconds giving the server time to shutdown gracefuly.
 	for seconds in {1..30}; do
 		check_status.sh
@@ -43,7 +43,7 @@ fn_stop_graceful_cmd() {
 	fn_print_dots "Graceful: sending \"${1}\""
 	fn_script_log_info "Graceful: sending \"${1}\""
 	# Sends specific stop command.
-	tmux send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1
 	# Waits up to ${seconds} seconds giving the server time to shutdown gracefully.
 	for ((seconds = 1; seconds <= ${2}; seconds++)); do
 		check_status.sh
@@ -71,7 +71,7 @@ fn_stop_graceful_goldsrc() {
 	fn_print_dots "Graceful: sending \"quit\""
 	fn_script_log_info "Graceful: sending \"quit\""
 	# sends quit
-	tmux send -t "${sessionname}" quit ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" send -t "${sessionname}" quit ENTER > /dev/null 2>&1
 	# Waits 3 seconds as goldsrc servers restart with the quit command.
 	for seconds in {1..3}; do
 		sleep 1
@@ -184,10 +184,10 @@ fn_stop_graceful_avorion() {
 	fn_print_dots "Graceful: /save /stop"
 	fn_script_log_info "Graceful: /save /stop"
 	# Sends /save.
-	tmux send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
 	sleep 5
 	# Sends /quit.
-	tmux send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
 	# Waits up to 30 seconds giving the server time to shutdown gracefuly.
 	for seconds in {1..30}; do
 		check_status.sh
@@ -240,7 +240,7 @@ fn_stop_tmux() {
 	fn_print_dots "${servername}"
 	fn_script_log_info "tmux kill-session: ${sessionname}: ${servername}"
 	# Kill tmux session.
-	tmux kill-session -t "${sessionname}" > /dev/null 2>&1
+	tmux -L "${sessionname}" kill-session -t "${sessionname}" > /dev/null 2>&1
 	sleep 0.5
 	check_status.sh
 	if [ "${status}" == "0" ]; then

+ 1 - 1
lgsm/functions/info_distro.sh

@@ -10,7 +10,7 @@ functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
 
 ### Game Server pid
 if [ "${status}" == "1" ]; then
-	gameserverpid="$(tmux list-sessions -F "#{session_name} #{pane_pid}" | grep "^${sessionname} " | awk '{print $NF}')"
+	gameserverpid="$(tmux -L "${sessionname}" list-sessions -F "#{session_name} #{pane_pid}" | grep "^${sessionname} " | awk '{print $NF}')"
 	if [ "${engine}" == "source" ]; then
 		srcdslinuxpid="$(ps -ef | grep -v grep | grep "${gameserverpid}" | grep srcds_linux | awk '{print $2}')"
 	elif [ "${engine}" == "goldsrc" ]; then

+ 1 - 1
lgsm/modules/check_status.sh

@@ -7,4 +7,4 @@
 
 moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
 
-status=$(tmux list-sessions -F "#{session_name}" 2> /dev/null | grep -Ecx "^${sessionname}")
+status=$(tmux -L "${sessionname}" list-sessions -F "#{session_name}" 2> /dev/null | grep -Ecx "^${sessionname}")

+ 1 - 1
lgsm/modules/command_console.sh

@@ -42,7 +42,7 @@ check_status.sh
 if [ "${status}" != "0" ]; then
 	fn_print_ok_nl "Accessing console"
 	fn_script_log_pass "Console accessed"
-	tmux attach-session -t "${sessionname}"
+	tmux -L "${sessionname}" attach-session -t "${sessionname}"
 	fn_print_ok_nl "Closing console"
 	fn_script_log_pass "Console closed"
 else

+ 1 - 1
lgsm/modules/command_send.sh

@@ -26,7 +26,7 @@ if [ "${status}" != "0" ]; then
 	fi
 	echo ""
 	fn_print_dots "Sending command to console: \"${commandtosend}\""
-	tmux send-keys -t "${servicename}" "${commandtosend}" ENTER
+	tmux -L "${sessionname}" send-keys -t "${servicename}" "${commandtosend}" ENTER
 	fn_print_ok_nl "Sending command to console: \"${commandtosend}\""
 	fn_script_log_pass "Command \"${commandtosend}\" sent to console"
 else

+ 7 - 7
lgsm/modules/command_start.sh

@@ -37,7 +37,7 @@ fn_start_teamspeak3() {
 # Used to allow update to detect JK2MV server version.
 fn_start_jk2() {
 	fn_start_tmux
-	tmux send -t "${sessionname}" version ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" end -t "${sessionname}" version ENTER > /dev/null 2>&1
 }
 
 fn_start_tmux() {
@@ -80,7 +80,7 @@ fn_start_tmux() {
 		cd "${executabledir}" || exit
 	fi
 
-	tmux new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
+	tmux -L "${sessionname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
 
 	# Create logfile.
 	touch "${consolelog}"
@@ -94,9 +94,9 @@ fn_start_tmux() {
 		echo -e "tmux version: master (user compiled)" >> "${consolelog}"
 		if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
 			if [ "$logtimestamp" == "on" ]; then
-				tmux pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
+				tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
 			else
-				tmux pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
+				tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
 			fi
 		fi
 
@@ -115,9 +115,9 @@ fn_start_tmux() {
 		# Console logging enable or not set.
 		elif [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
 			if [ "$logtimestamp" == "on" ]; then
-				tmux pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
+				tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
 			else
-				tmux pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
+				tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
 			fi
 		fi
 	else
@@ -143,7 +143,7 @@ fn_start_tmux() {
 			echo -e ""
 			echo -e "Command"
 			echo -e "================================="
-			echo -e "tmux new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
+			echo -e "tmux -L \"${sessionname}\" new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
 			echo -e ""
 			echo -e "Error"
 			echo -e "================================="

+ 6 - 6
lgsm/modules/command_stop.sh

@@ -15,7 +15,7 @@ fn_stop_graceful_ctrlc() {
 	fn_print_dots "Graceful: CTRL+c"
 	fn_script_log_info "Graceful: CTRL+c"
 	# Sends quit.
-	tmux send-keys -t "${sessionname}" C-c > /dev/null 2>&1
+	tmux -L "${sessionname}" send-keys -t "${sessionname}" C-c > /dev/null 2>&1
 	# Waits up to 30 seconds giving the server time to shutdown gracefuly.
 	for seconds in {1..30}; do
 		check_status.sh
@@ -43,7 +43,7 @@ fn_stop_graceful_cmd() {
 	fn_print_dots "Graceful: sending \"${1}\""
 	fn_script_log_info "Graceful: sending \"${1}\""
 	# Sends specific stop command.
-	tmux send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1
 	# Waits up to ${seconds} seconds giving the server time to shutdown gracefully.
 	for ((seconds = 1; seconds <= ${2}; seconds++)); do
 		check_status.sh
@@ -71,7 +71,7 @@ fn_stop_graceful_goldsrc() {
 	fn_print_dots "Graceful: sending \"quit\""
 	fn_script_log_info "Graceful: sending \"quit\""
 	# sends quit
-	tmux send -t "${sessionname}" quit ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" send -t "${sessionname}" quit ENTER > /dev/null 2>&1
 	# Waits 3 seconds as goldsrc servers restart with the quit command.
 	for seconds in {1..3}; do
 		sleep 1
@@ -184,10 +184,10 @@ fn_stop_graceful_avorion() {
 	fn_print_dots "Graceful: /save /stop"
 	fn_script_log_info "Graceful: /save /stop"
 	# Sends /save.
-	tmux send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
 	sleep 5
 	# Sends /quit.
-	tmux send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
+	tmux -L "${sessionname}" send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
 	# Waits up to 30 seconds giving the server time to shutdown gracefuly.
 	for seconds in {1..30}; do
 		check_status.sh
@@ -240,7 +240,7 @@ fn_stop_tmux() {
 	fn_print_dots "${servername}"
 	fn_script_log_info "tmux kill-session: ${sessionname}: ${servername}"
 	# Kill tmux session.
-	tmux kill-session -t "${sessionname}" > /dev/null 2>&1
+	tmux -L "${sessionname}" kill-session -t "${sessionname}" > /dev/null 2>&1
 	sleep 0.5
 	check_status.sh
 	if [ "${status}" == "0" ]; then

+ 1 - 1
lgsm/modules/info_distro.sh

@@ -10,7 +10,7 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
 
 ### Game Server pid
 if [ "${status}" == "1" ]; then
-	gameserverpid="$(tmux list-sessions -F "#{session_name} #{pane_pid}" | grep "^${sessionname} " | awk '{print $NF}')"
+	gameserverpid="$(tmux -L "${sessionname}" list-sessions -F "#{session_name} #{pane_pid}" | grep "^${sessionname} " | awk '{print $NF}')"
 	if [ "${engine}" == "source" ]; then
 		srcdslinuxpid="$(ps -ef | grep -v grep | grep "${gameserverpid}" | grep srcds_linux | awk '{print $2}')"
 	elif [ "${engine}" == "goldsrc" ]; then