From de9cefd6310faf7b518ca275c7310157903bc4a6 Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Wed, 6 Sep 2023 21:40:58 +0100 Subject: [PATCH] fix: make pgrep unique - Refactored the code to use `pgrep -fcx` instead of `pgrep -fc` for checking process counts. - Updated print statements to provide more informative messages. - Renamed some variables for clarity. - Added newlines after printing information. --- lgsm/modules/command_monitor.sh | 70 ++++++++++++++++----------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/lgsm/modules/command_monitor.sh b/lgsm/modules/command_monitor.sh index c64df9fda8..ebda8aebf0 100644 --- a/lgsm/modules/command_monitor.sh +++ b/lgsm/modules/command_monitor.sh @@ -26,69 +26,69 @@ fn_monitor_check_monitoring() { } fn_monitor_check_install() { - if [ "$(pgrep -fc -u "${USER}" "${selfname} install")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} i")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} auto-install")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} ai")" != "0" ]; then + if [ "$(pgrep -fcx -u "${USER}" "${selfname} install")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "${selfname} i")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "${selfname} auto-install")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "${selfname} ai")" != "0" ]; then fn_print_dots "Checking installer: " fn_print_checking_eol fn_script_log_info "Checking installer: CHECKING" - fn_print_info "Checking installer: Installer is : " - fn_print_info_eol + fn_print_info "Checking installer: LinuxGSM is installing: " + fn_print_info_eol_nl fn_script_log_pass "Checking installer: LinuxGSM is installing" core_exit.sh fi } fn_monitor_check_debug() { - if [ "$(pgrep -fc -u "${USER}" "${selfname} debug")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} d")" != "0" ]; then + if [ "$(pgrep -fcx -u "${USER}" "${selfname} debug")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "${selfname} d")" != "0" ]; then fn_print_dots "Checking debug: " fn_print_checking_eol fn_print_info "Checking debug: Debug is running: " - fn_print_info_eol + fn_print_info_eol_nl fn_script_log_pass "Checking debug: Debug is running" core_exit.sh fi } -fn_monitor_check_starting(){ +fn_monitor_check_starting() { # Remove stale lockfile. if [ -f "${lockdir}/${selfname}-starting.lock" ]; then if [ "$(find "${lockdir}/${selfname}-starting.lock" -mmin +5)" ]; then fn_print_dots "Checking start: " fn_print_checking_eol fn_print_warn "Checking start: Removing stale lockfile: " - fn_print_warn_eol + fn_print_warn_eol_nl fn_script_log_warn "Checking start: Removing stale lockfile" rm -f "${lockdir:?}/${selfname}-starting.lock" fi fi - if [ -f "${lockdir}/${selfname}-starting.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} start")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} s")" != "0" ]]; then + if [ -f "${lockdir}/${selfname}-starting.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "${selfname} start")" != "0" || "$(pgrep -fcx -u "${USER}" "${selfname} s")" != "0" ]]; then fn_print_dots "Checking start: " fn_print_checking_eol fn_print_info "Checking start: LinuxGSM is starting: " - fn_print_info_eol + fn_print_info_eol_nl fn_script_log_info "Checking backup: LinuxGSM is starting" core_exit.sh fi } -fn_monitor_check_stopping(){ +fn_monitor_check_stopping() { # Remove stale lockfile. if [ -f "${lockdir}/${selfname}-stopping.lock" ]; then if [ "$(find "${lockdir}/${selfname}-stopping.lock" -mmin +5)" ]; then fn_print_dots "Checking stop: " fn_print_checking_eol fn_print_warn "Checking stop: Removing stale lockfile: " - fn_print_warn_eol + fn_print_warn_eol_nl fn_script_log_warn "Checking stop: Removing stale lockfile" rm -f "${lockdir:?}/${selfname}-stopping.lock" fi fi - if [ -f "${lockdir}/${selfname}-stopping.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} stop")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} s")" != "0" ]]; then + if [ -f "${lockdir}/${selfname}-stopping.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "${selfname} stop")" != "0" || "$(pgrep -fcx -u "${USER}" "${selfname} s")" != "0" ]]; then fn_print_dots "Checking stop: " fn_print_checking_eol fn_print_info "Checking stop: LinuxGSM is stopping: " - fn_print_info_eol + fn_print_info_eol_nl fn_script_log_info "Checking backup: LinuxGSM is stopping" core_exit.sh fi @@ -107,11 +107,11 @@ fn_monitor_check_backup() { fi fi - if [ -f "${lockdir}/backup.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} backup")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} b")" != "0" ]]; then + if [ -f "${lockdir}/backup.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "${selfname} backup")" != "0" || "$(pgrep -fcx -u "${USER}" "${selfname} b")" != "0" ]]; then fn_print_dots "Checking backup: " fn_print_checking_eol fn_print_info "Checking backup: Backup is running: " - fn_print_info_eol + fn_print_info_eol_nl fn_script_log_info "Checking backup: Backup is running" core_exit.sh fi @@ -124,38 +124,38 @@ fn_monitor_check_update() { fn_print_dots "Checking update: " fn_print_checking_eol fn_print_warn "Checking update: Removing stale lockfile: " - fn_print_warn_eol + fn_print_warn_eol_nl fn_script_log_warn "Checking update: Removing stale lockfile" rm -f "${lockdir:?}/update.lock" fi fi - if [ -f "${lockdir}/update.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} update")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} validate")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} v")" != "0" || "$(pgrep -fc force-update "${USER}" "${selfname} fu")" != "0" ]]; then + if [ -f "${lockdir}/update.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "${selfname} update")" != "0" || "$(pgrep -fcx -u "${USER}" "${selfname} validate")" != "0" || "$(pgrep -fcx -u "${USER}" "${selfname} v")" != "0" || "$(pgrep -fc force-update "${USER}" "${selfname} fu")" != "0" ]]; then fn_print_dots "Checking update: " fn_print_checking_eol fn_print_info "Checking update: LinuxGSM is updating the game server: " - fn_print_info_eol + fn_print_info_eol_nl fn_script_log_pass "Checking update: LinuxGSM is updating the game server" core_exit.sh fi } # Source engine games may display a messages to indicate the server needs restarting. -fn_monitor_check_update_source(){ - if [ -f "${consolelogdir}/${selfname}-console.log" ] && [ "${engine}" == "source" ]; then - if grep -q "Your server needs to be restarted in order to receive the latest update." "${consolelogdir}/${selfname}-console.log"; then - fn_print_dots "Checking update: " - fn_print_checking_eol - fn_script_log_info "Checking update: CHECKING" - fn_print_ok "Checking update: " - fn_print_ok_eol_nl - fn_script_log_info "Checking update: Monitor is restarting ${selfname} to apply update" - alert="restart" - alert.sh - command_restart.sh - core_exit.sh - fi - fi +fn_monitor_check_update_source() { + if [ -f "${consolelogdir}/${selfname}-console.log" ] && [ "${engine}" == "source" ]; then + if grep -q "Your server needs to be restarted in order to receive the latest update." "${consolelogdir}/${selfname}-console.log"; then + fn_print_dots "Checking update: " + fn_print_checking_eol + fn_script_log_info "Checking update: CHECKING" + fn_print_ok "Checking update: " + fn_print_ok_eol_nl + fn_script_log_info "Checking update: Monitor is restarting ${selfname} to apply update" + alert="restart" + alert.sh + command_restart.sh + core_exit.sh + fi + fi } fn_monitor_check_session() { @@ -166,7 +166,7 @@ fn_monitor_check_session() { sessionwidth="80" sessionheight="23" # Check for PIDS with identical tmux sessions running. - if [ "$(pgrep -fc "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" -ge "2" ]; then + if [ "$(pgrep -fcx "tmux -L ${socketname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" -ge "2" ]; then fn_print_error "Checking session: " fn_print_error_eol_nl fn_script_log_error "Checking session: ERROR" @@ -176,7 +176,7 @@ fn_monitor_check_session() { command_restart.sh core_exit.sh # Check for tmux pids with the same tmux session and socket names. This will reduce issues with migration to release v23.5.0. #4296 - elif [ "$(pgrep -fc -u "${USER}" "tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" != "0" ]; then + elif [ "$(pgrep -fcx -u "${USER}" "tmux -L ${sessionname} new-session -d -x ${sessionwidth} -y ${sessionheight} -s ${sessionname}")" != "0" ]; then fn_print_error "Checking session: " fn_print_error_eol_nl fn_script_log_error "Checking session: ERROR"