From 0e5a9947f4213d8452f501c1b49269ba018cad31 Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Mon, 4 Sep 2023 20:42:57 +0100 Subject: [PATCH] refactor: remove redundant code and improve socket name generation The commit refactors the code in `command_start.sh` and `linuxgsm.sh` to remove redundant code related to generating a unique tmux socket name. The code now creates a uid file if it doesn't exist, stops the running server, generates a uid using the current timestamp, and updates the socketname accordingly. This change improves the efficiency and reliability of socket name generation. --- lgsm/modules/command_start.sh | 12 +----------- linuxgsm.sh | 13 ++++++++++++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lgsm/modules/command_start.sh b/lgsm/modules/command_start.sh index 1302d0d99..cabccac47 100644 --- a/lgsm/modules/command_start.sh +++ b/lgsm/modules/command_start.sh @@ -48,16 +48,6 @@ fn_start_tmux() { fn_reload_startparameters - # Create uid to ensure unique tmux socket name. - if [ ! -f "${datadir}/${selfname}.uid" ]; then - # stop running server (if running) to prevent lingering tmux sessions. - exitbypass=1 - command_stop.sh - uid=$(date '+%s' | sha1sum | head -c 8) - echo "${uid}" > "${datadir}/${selfname}.uid" - socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")" - fi - if [ "${shortname}" == "av" ]; then cd "${systemdir}" || exit else @@ -181,7 +171,7 @@ fn_start_tmux() { check.sh # If user ran the start command monitor will become enabled. -if [ "${firstcommandname}" == "START" ]||[ "${firstcommandname}" == "RESTART" ]; then +if [ "${firstcommandname}" == "START" ] || [ "${firstcommandname}" == "RESTART" ]; then date '+%s' > "${lockdir:?}/${selfname}-monitoring.lock" fi diff --git a/linuxgsm.sh b/linuxgsm.sh index 331a61822..03ae4a434 100755 --- a/linuxgsm.sh +++ b/linuxgsm.sh @@ -36,7 +36,7 @@ tmpdir="${lgsmdir}/tmp" datadir="${lgsmdir}/data" lockdir="${lgsmdir}/lock" sessionname="${selfname}" -[ -f "${datadir}/${selfname}.uid" ] && socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")" || socketname="${sessionname}" +socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")" serverlist="${datadir}/serverlist.csv" serverlistmenu="${datadir}/serverlistmenu.csv" [ -n "${LGSM_CONFIG}" ] && configdir="${LGSM_CONFIG}" || configdir="${lgsmdir}/config-lgsm" @@ -413,6 +413,17 @@ else fi fi fi + + # Create uid to ensure unique tmux socket name. + if [ ! -f "${datadir}/${selfname}.uid" ]; then + # stop running server (if running) to prevent lingering tmux sessions. + exitbypass=1 + command_stop.sh + uid=$(date '+%s' | sha1sum | head -c 8) + echo "${uid}" > "${datadir}/${selfname}.uid" + socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")" + fi + # Load the IP details before the first config is loaded. check_ip.sh # Configs have to be loaded twice to allow start startparameters to pick up all vars