Skip to content

Commit 4ddd8ee

Browse files
authored
feat: tmux uniqueness (#4296)
* refactor: refactor tmux session handling - Updated the code to use the variable `socketname` instead of `sessionname` when interacting with tmux sessions. - Added logic to create a unique socket name using a UID if it doesn't already exist. - Refactored multiple functions (`fn_start_jk2`, `fn_start_tmux`, `fn_stop_graceful_ctrlc`, `fn_stop_graceful_cmd`, `fn_stop_graceful_goldsrc`, `fn_stop_graceful_avorion`, and `fn_stop_tmux`) to use the new socket name for tmux commands. This commit improves the readability and maintainability of the code by separating the concept of session names from socket names, allowing for more flexibility in managing tmux sessions. * feat: stop server when generating uid this should prevent issues when migrating to using uid * refactor: lockfile handling - Removed unnecessary function fn_start_jk2() - Removed unused variable startparameters in fn_start_tmux() - Added comments to clarify code functionality - Renamed lockfile from "${lockdir}/${selfname}.lock" to "${lockdir}/${selfname}-start.lock" - Updated log messages and log file names to include timestamp * refactor: improve backup and update monitoring The code in `command_monitor.sh` has been refactored to enhance the monitoring of backups and updates. The conditions for checking if a backup or update is running have been modified to include additional checks using `pgrep`. This change improves the accuracy of the monitoring process. * refactor: improve logging in core_exit.sh - Changed the grep command to include the -a flag to handle non-text files - Updated sed command to remove "modulefile=" from the output and redirect it to dev-debug-module-order.log * refactor: improve code readability and remove redundant code - Refactored the `fn_monitor_check_starting` function to check for stale lockfiles and print appropriate messages. - Refactored the `fn_monitor_check_stopping` function to check for stale lockfiles and print appropriate messages. - Removed redundant code that deletes the update lockfile in `command_update.sh` and `command_validate.sh`. - Improved code readability by removing unnecessary comments. * feat: add exitbypass flag to prevent lingering tmux sessions The code changes in `command_start.sh` include adding an `exitbypass` flag to prevent lingering tmux sessions. This flag is used to stop the running server before creating a unique uid for the tmux socket name. * refactor: Refactor lockfile names for consistency and clarity - Renamed "${lockdir}/${selfname}-laststart.lock" to "${lockdir}/${selfname}-last-started.lock" - Renamed "${lockdir}/backup.lock" to "${lockdir}/stopping.lock" - Renamed "${lockdir}/${selfname}-start.lock" to "${lockdir}/${selfname}-started.lock" - Renamed "${lockdir}/${selfname}-starting.lock" to "${lockdir}/${selfname}-stopping.lock" This commit refactors the lockfile names in the codebase for better consistency and clarity. The changes make it easier to understand the purpose of each lockfile and improve readability. * refactor: refactor check_last_update.sh, command_backup.sh, command_debug.sh, command_mods_remove.sh, command_monitor.sh, command_start.sh, command_stop.sh, command_ts3_server_pass.sh and core_steamcmd.sh - Refactored code to improve readability and maintainability. - Renamed variables for clarity. - Removed unnecessary comments and unused code. - Fixed typos and formatting issues. * refactor: update lock file path in core_steamcmd.sh The lock file path in the core_steamcmd.sh script has been updated to use the correct variable. This change ensures that the lock file is created in the correct directory. * refactor: improve file deletion in command_stop.sh The code change refactors the file deletion process in the command_stop.sh script. Instead of using an empty redirect, it now directly removes the specified lock file. This improves clarity and ensures proper cleanup when stopping a command. * refactor: update lockfile name in command_monitor.sh The lockfile name has been changed from "${selfname}-started.lock" to "${selfname}-monitoring.lock" in the fn_monitor_check_lockfile function. The code has also been refactored to remove unnecessary checks and fixes for the lockfile. * refactor: simplify start and stop command checks The code in `command_start.sh` and `command_stop.sh` has been refactored to simplify the checks for the start and stop commands. Instead of checking if `exitbypass` is empty, it now directly checks if `firstcommandname` matches specific values ("START", "RESTART" for start command, and "STOP" for stop command). This change improves readability and reduces unnecessary conditions. * refactor: simplify lockfile names and remove duplicate code - Renamed lockfiles from "${selfname}-started.lock" to "started.lock", "${selfname}-starting.lock" to "starting.lock", "${selfname}-stopping.lock" to "stopping.lock", and "${selfname}-update.lock" to "update.lock". - Removed duplicate code for removing stale lockfiles in fn_monitor_check_starting(), fn_monitor_check_stopping(), fn_monitor_check_backup(), fn_monitor_check_update(), update_factorio.sh, update_jediknight2.sh, update_minecraft.sh, update_minecraft_bedrock.sh, update_mta.sh, update_papermc.sh, update_ts3.sh, update_ut99.sh, and update_vintagestory.sh. - Updated references to the renamed lockfiles in the respective functions. * fix: corrected commandname * fix: commandname skeleton * refactor: improve file path handling in check_last_update.sh The code changes refactor the file path handling in check_last_update.sh to use the correct lock directory. This ensures that the last update time is correctly retrieved and compared with the last start time. * feat: add update lock file Add functionality to create an update lock file when validating and updating SteamCMD. This prevents potential conflicts with customised files. - Create a lock file with the current timestamp in the "command_validate.sh" script - Create a lock file with the current timestamp in the "update_steamcmd.sh" script * refactor: rename libgcc_s.so.1 backup file The code has been refactored to improve clarity and readability. The variable `libgccc_so` has been renamed to `libgccc_so.bak` for better understanding of its purpose. This change ensures that the backup file for libgcc_s.so.1 is correctly named and easily identifiable. * refactor: improve logging and error handling in command_monitor.sh - Added error handling for PIDs with identical tmux sessions running, killing them and restarting the server. - Added error handling for PIDs with the same tmux session and socket names running, killing them and restarting the server.
1 parent 290eb6b commit 4ddd8ee

31 files changed

+328
-152
lines changed

lgsm/modules/check_last_update.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
# Author: Daniel Gibbs
44
# Contributors: http://linuxgsm.com/contrib
55
# Website: https://linuxgsm.com
6-
# Description: Checks lock file to see when last update happened.
6+
# Description: Checks Lockfile to see when last update happened.
77
# Will reboot server if instance not rebooted since update.
88

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

11-
if [ -f "${lockdir}/${selfname}-laststart.lock" ]; then
12-
laststart=$(cat "${lockdir}/${selfname}-laststart.lock")
11+
if [ -f "${lockdir}/${selfname}-last-started.lock" ]; then
12+
laststart=$(cat "${lockdir}/${selfname}-last-started.lock")
1313
fi
14-
if [ -f "${lockdir}/lastupdate.lock" ]; then
15-
lastupdate=$(cat "${lockdir}/lastupdate.lock")
14+
if [ -f "${lockdir}/last-updated.lock" ]; then
15+
lastupdate=$(cat "${lockdir}/last-updated.lock")
1616
fi
1717

1818
check_status.sh
19-
if [ -f "${lockdir}/lastupdate.lock" ] && [ "${status}" != "0" ]; then
20-
if [ ! -f "${lockdir}/${selfname}-laststart.lock" ] || [ "${laststart}" -lt "${lastupdate}" ]; then
19+
if [ -f "${lockdir}/last-updated.lock" ] && [ "${status}" != "0" ]; then
20+
if [ ! -f "${lockdir}/${selfname}-last-started.lock" ] || [ "${laststart}" -lt "${lastupdate}" ]; then
2121
fn_print_info "${selfname} has not been restarted since last update"
2222
fn_script_log_info "${selfname} has not been restarted since last update"
2323
command_restart.sh

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

lgsm/modules/command_backup.sh

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn_backup_trap() {
2222
echo -en "backup ${backupname}.tar.gz..."
2323
fn_print_removed_eol_nl
2424
fn_script_log_info "Backup ${backupname}.tar.gz: REMOVED"
25-
# Remove lock file.
25+
# Remove backup lockfile.
2626
rm -f "${lockdir:?}/backup.lock"
2727
fn_backup_start_server
2828
unset exitbypass
@@ -31,9 +31,21 @@ fn_backup_trap() {
3131

3232
# Check if a backup is pending or has been aborted using backup.lock.
3333
fn_backup_check_lockfile() {
34+
# Remove stale lockfile.
3435
if [ -f "${lockdir}/backup.lock" ]; then
35-
fn_print_info_nl "Lock file found: Backup is currently running"
36-
fn_script_log_error "Lock file found: Backup is currently running: ${lockdir}/backup.lock"
36+
if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then
37+
fn_print_dots "Lockfile found: "
38+
fn_print_checking_eol
39+
fn_print_warn "Lockfile found: Removing stale lockfile: "
40+
fn_print_warn_eol
41+
fn_script_log_warn "Lockfile found: Removing stale lockfile"
42+
rm -f "${lockdir:?}/backup.lock"
43+
fi
44+
fi
45+
46+
if [ -f "${lockdir}/backup.lock" ]; then
47+
fn_print_info_nl "Lockfile found: Backup is currently running"
48+
fn_script_log_error "Lockfile found: Backup is currently running: ${lockdir}/backup.lock"
3749
core_exit.sh
3850
fi
3951
}
@@ -117,8 +129,8 @@ fn_backup_migrate_olddir() {
117129

118130
fn_backup_create_lockfile() {
119131
# Create lockfile.
120-
date '+%s' > "${lockdir}/backup.lock"
121-
fn_script_log_info "Lockfile generated"
132+
date '+%s' > "${lockdir:?}/backup.lock"
133+
fn_script_log_info "Backup lockfile generated"
122134
fn_script_log_info "${lockdir}/backup.lock"
123135
# trap to remove lockfile on quit.
124136
trap fn_backup_trap INT
@@ -140,7 +152,7 @@ fn_backup_compression() {
140152
core_exit.sh
141153
fi
142154

143-
tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}/backup.lock" ./.
155+
tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}" ./.
144156
local exitcode=$?
145157
if [ "${exitcode}" != 0 ]; then
146158
fn_print_fail_eol
@@ -153,8 +165,6 @@ fn_backup_compression() {
153165
fn_print_ok_nl "Completed: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
154166
fn_script_log_pass "Backup created: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
155167
fi
156-
# Remove lock file
157-
rm -f "${lockdir:?}/backup.lock"
158168
}
159169

160170
# Clear old backups according to maxbackups and maxbackupdays variables.
@@ -265,4 +275,6 @@ fn_backup_compression
265275
fn_backup_prune
266276
fn_backup_start_server
267277

278+
# Remove backup lockfile.
279+
rm -f "${lockdir:?}/backup.lock"
268280
core_exit.sh

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 -L "${sessionname}" attach-session -t "${sessionname}"
45+
tmux -L "${socketname}" attach-session -t "${sessionname}"
4646
fn_print_ok_nl "Closing console"
4747
fn_script_log_pass "Console closed"
4848
else

lgsm/modules/command_debug.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn_firstcommand_set
1313
# Trap to remove lockfile on quit.
1414
fn_lockfile_trap() {
1515
# Remove lockfile.
16-
rm -f "${lockdir:?}/${selfname}.lock"
16+
rm -f "${lockdir:?}/${selfname}-started.lock"
1717
# resets terminal. Servers can sometimes mess up the terminal on exit.
1818
reset
1919
fn_print_dots "Stopping debug"
@@ -98,12 +98,12 @@ fn_print_dots "Starting debug"
9898
fn_script_log_info "Starting debug"
9999
fn_print_ok_nl "Starting debug"
100100

101-
# Create lockfile.
102-
date '+%s' > "${lockdir}/${selfname}.lock"
103-
echo "${version}" >> "${lockdir}/${selfname}.lock"
104-
echo "${port}" >> "${lockdir}/${selfname}.lock"
101+
# Create started lockfile.
102+
date '+%s' > "${lockdir:?}/${selfname}-started.lock"
103+
echo "${version}" >> "${lockdir}/${selfname}-started.lock"
104+
echo "${port}" >> "${lockdir}/${selfname}-started.lock"
105105
fn_script_log_info "Lockfile generated"
106-
fn_script_log_info "${lockdir}/${selfname}.lock"
106+
fn_script_log_info "${lockdir}/${selfname}-started.lock"
107107

108108
if [ "${shortname}" == "av" ]; then
109109
cd "${systemdir}" || exit

lgsm/modules/command_mods_remove.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fi
131131
if [ "${engine}" == "unity3d" ] && [[ "${modprettyname}" == *"Oxide"* ]]; then
132132
fn_print_information_nl "Validating to restore original ${gamename} files replaced by Oxide"
133133
fn_script_log "Validating to restore original ${gamename} files replaced by Oxide"
134-
exitbypass="1"
134+
exitbypass=1
135135
command_validate.sh
136136
fn_firstcommand_reset
137137
unset exitbypass

0 commit comments

Comments
 (0)