Skip to content

Commit e9f2453

Browse files
ccleclecclecledgibbs64
authored
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>
1 parent ab09a32 commit e9f2453

12 files changed

+32
-32
lines changed

lgsm/functions/check_status.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
99

10-
status=$(tmux list-sessions -F "#{session_name}" 2> /dev/null | grep -Ecx "^${sessionname}")
10+
status=$(tmux -L "${sessionname}" list-sessions -F "#{session_name}" 2>/dev/null | grep -Ecx "^${sessionname}")

lgsm/functions/command_console.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ check_status.sh
4242
if [ "${status}" != "0" ]; then
4343
fn_print_ok_nl "Accessing console"
4444
fn_script_log_pass "Console accessed"
45-
tmux attach-session -t "${sessionname}"
45+
tmux -L "${sessionname}" attach-session -t "${sessionname}"
4646
fn_print_ok_nl "Closing console"
4747
fn_script_log_pass "Console closed"
4848
else

lgsm/functions/command_send.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if [ "${status}" != "0" ]; then
2626
fi
2727
echo ""
2828
fn_print_dots "Sending command to console: \"${commandtosend}\""
29-
tmux send-keys -t "${servicename}" "${commandtosend}" ENTER
29+
tmux -L "${sessionname}" send-keys -t "${servicename}" "${commandtosend}" ENTER
3030
fn_print_ok_nl "Sending command to console: \"${commandtosend}\""
3131
fn_script_log_pass "Command \"${commandtosend}\" sent to console"
3232
else

lgsm/functions/command_start.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn_start_teamspeak3() {
3737
# Used to allow update to detect JK2MV server version.
3838
fn_start_jk2() {
3939
fn_start_tmux
40-
tmux send -t "${sessionname}" version ENTER > /dev/null 2>&1
40+
tmux -L "${sessionname}" send -t "${sessionname}" version ENTER > /dev/null 2>&1
4141
}
4242

4343
fn_start_tmux() {
@@ -80,7 +80,7 @@ fn_start_tmux() {
8080
cd "${executabledir}" || exit
8181
fi
8282

83-
tmux new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
83+
tmux -L "${sessionname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
8484

8585
# Create logfile.
8686
touch "${consolelog}"
@@ -94,9 +94,9 @@ fn_start_tmux() {
9494
echo -e "tmux version: master (user compiled)" >> "${consolelog}"
9595
if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
9696
if [ "$logtimestamp" == "on" ]; then
97-
tmux pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
97+
tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
9898
else
99-
tmux pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
99+
tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
100100
fi
101101
fi
102102

@@ -143,7 +143,7 @@ fn_start_tmux() {
143143
echo -e ""
144144
echo -e "Command"
145145
echo -e "================================="
146-
echo -e "tmux new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
146+
echo -e "tmux -L \"${sessionname}\" new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
147147
echo -e ""
148148
echo -e "Error"
149149
echo -e "================================="

lgsm/functions/command_stop.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn_stop_graceful_ctrlc() {
1515
fn_print_dots "Graceful: CTRL+c"
1616
fn_script_log_info "Graceful: CTRL+c"
1717
# Sends quit.
18-
tmux send-keys -t "${sessionname}" C-c > /dev/null 2>&1
18+
tmux -L "${sessionname}" send-keys -t "${sessionname}" C-c > /dev/null 2>&1
1919
# Waits up to 30 seconds giving the server time to shutdown gracefuly.
2020
for seconds in {1..30}; do
2121
check_status.sh
@@ -43,7 +43,7 @@ fn_stop_graceful_cmd() {
4343
fn_print_dots "Graceful: sending \"${1}\""
4444
fn_script_log_info "Graceful: sending \"${1}\""
4545
# Sends specific stop command.
46-
tmux send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1
46+
tmux -L "${sessionname}" send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1
4747
# Waits up to ${seconds} seconds giving the server time to shutdown gracefully.
4848
for ((seconds = 1; seconds <= ${2}; seconds++)); do
4949
check_status.sh
@@ -71,7 +71,7 @@ fn_stop_graceful_goldsrc() {
7171
fn_print_dots "Graceful: sending \"quit\""
7272
fn_script_log_info "Graceful: sending \"quit\""
7373
# sends quit
74-
tmux send -t "${sessionname}" quit ENTER > /dev/null 2>&1
74+
tmux -L "${sessionname}" send -t "${sessionname}" quit ENTER > /dev/null 2>&1
7575
# Waits 3 seconds as goldsrc servers restart with the quit command.
7676
for seconds in {1..3}; do
7777
sleep 1
@@ -184,10 +184,10 @@ fn_stop_graceful_avorion() {
184184
fn_print_dots "Graceful: /save /stop"
185185
fn_script_log_info "Graceful: /save /stop"
186186
# Sends /save.
187-
tmux send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
187+
tmux -L "${sessionname}" send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
188188
sleep 5
189189
# Sends /quit.
190-
tmux send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
190+
tmux -L "${sessionname}" send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
191191
# Waits up to 30 seconds giving the server time to shutdown gracefuly.
192192
for seconds in {1..30}; do
193193
check_status.sh
@@ -240,7 +240,7 @@ fn_stop_tmux() {
240240
fn_print_dots "${servername}"
241241
fn_script_log_info "tmux kill-session: ${sessionname}: ${servername}"
242242
# Kill tmux session.
243-
tmux kill-session -t "${sessionname}" > /dev/null 2>&1
243+
tmux -L "${sessionname}" kill-session -t "${sessionname}" > /dev/null 2>&1
244244
sleep 0.5
245245
check_status.sh
246246
if [ "${status}" == "0" ]; then

lgsm/functions/info_distro.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
1010

1111
### Game Server pid
1212
if [ "${status}" == "1" ]; then
13-
gameserverpid="$(tmux list-sessions -F "#{session_name} #{pane_pid}" | grep "^${sessionname} " | awk '{print $NF}')"
13+
gameserverpid="$(tmux -L "${sessionname}" list-sessions -F "#{session_name} #{pane_pid}" | grep "^${sessionname} " | awk '{print $NF}')"
1414
if [ "${engine}" == "source" ]; then
1515
srcdslinuxpid="$(ps -ef | grep -v grep | grep "${gameserverpid}" | grep srcds_linux | awk '{print $2}')"
1616
elif [ "${engine}" == "goldsrc" ]; then

lgsm/modules/check_status.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
99

10-
status=$(tmux list-sessions -F "#{session_name}" 2> /dev/null | grep -Ecx "^${sessionname}")
10+
status=$(tmux -L "${sessionname}" list-sessions -F "#{session_name}" 2> /dev/null | grep -Ecx "^${sessionname}")

lgsm/modules/command_console.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ check_status.sh
4242
if [ "${status}" != "0" ]; then
4343
fn_print_ok_nl "Accessing console"
4444
fn_script_log_pass "Console accessed"
45-
tmux attach-session -t "${sessionname}"
45+
tmux -L "${sessionname}" attach-session -t "${sessionname}"
4646
fn_print_ok_nl "Closing console"
4747
fn_script_log_pass "Console closed"
4848
else

lgsm/modules/command_send.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if [ "${status}" != "0" ]; then
2626
fi
2727
echo ""
2828
fn_print_dots "Sending command to console: \"${commandtosend}\""
29-
tmux send-keys -t "${servicename}" "${commandtosend}" ENTER
29+
tmux -L "${sessionname}" send-keys -t "${servicename}" "${commandtosend}" ENTER
3030
fn_print_ok_nl "Sending command to console: \"${commandtosend}\""
3131
fn_script_log_pass "Command \"${commandtosend}\" sent to console"
3232
else

lgsm/modules/command_start.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn_start_teamspeak3() {
3737
# Used to allow update to detect JK2MV server version.
3838
fn_start_jk2() {
3939
fn_start_tmux
40-
tmux send -t "${sessionname}" version ENTER > /dev/null 2>&1
40+
tmux -L "${sessionname}" end -t "${sessionname}" version ENTER > /dev/null 2>&1
4141
}
4242

4343
fn_start_tmux() {
@@ -80,7 +80,7 @@ fn_start_tmux() {
8080
cd "${executabledir}" || exit
8181
fi
8282

83-
tmux new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
83+
tmux -L "${sessionname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
8484

8585
# Create logfile.
8686
touch "${consolelog}"
@@ -94,9 +94,9 @@ fn_start_tmux() {
9494
echo -e "tmux version: master (user compiled)" >> "${consolelog}"
9595
if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
9696
if [ "$logtimestamp" == "on" ]; then
97-
tmux pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
97+
tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
9898
else
99-
tmux pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
99+
tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
100100
fi
101101
fi
102102

@@ -115,9 +115,9 @@ fn_start_tmux() {
115115
# Console logging enable or not set.
116116
elif [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
117117
if [ "$logtimestamp" == "on" ]; then
118-
tmux pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
118+
tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
119119
else
120-
tmux pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
120+
tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
121121
fi
122122
fi
123123
else
@@ -143,7 +143,7 @@ fn_start_tmux() {
143143
echo -e ""
144144
echo -e "Command"
145145
echo -e "================================="
146-
echo -e "tmux new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
146+
echo -e "tmux -L \"${sessionname}\" new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
147147
echo -e ""
148148
echo -e "Error"
149149
echo -e "================================="

0 commit comments

Comments
 (0)