Skip to content

Commit 76491eb

Browse files
borzakaclaudedgibbs64
authored
fix(monitor): detect running commands when the script is run by its full path (#5015)
Monitor only found commands started as ./gameserver <command>, because the checks used pgrep -x with the /bin/bash ./ prefix. The cron, systemd and Docker examples in the docs run the script by its full path, so monitor missed updates and backups and restarted the server in the middle of them. One helper now builds a single pattern per check that accepts any path to the script. Fixes #5013. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Daniel Gibbs <me@danielgibbs.co.uk>
1 parent 6017300 commit 76491eb

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎lgsm/modules/command_monitor.sh‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ commandaction="Monitoring"
1111
moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
1212
fn_firstcommand_set
1313

14+
# Checks if this instance is running one of the given commands, e.g. "update|u".
15+
# Matches the script run as ./gameserver, /path/to/gameserver or bash gameserver.
16+
fn_monitor_command_running() {
17+
[ "$(pgrep -fc -u "${USER}" "^([^ ]*/)?bash ([^ ]*/)?${selfname} ($1)( |$)")" != "0" ]
18+
}
19+
1420
fn_monitor_check_monitoring() {
1521
if [ -f "${lockdir}/${selfname}.lock" ]; then
1622
# Part of migration to v23.5.0. #4296
@@ -30,7 +36,7 @@ fn_monitor_check_monitoring() {
3036
}
3137

3238
fn_monitor_check_install() {
33-
if [ "$(pgrep -fc -u "${USER}" "/bin/bash ./${selfname} install")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} i")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} auto-install")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} ai")" != "0" ]; then
39+
if fn_monitor_command_running "install|i|auto-install|ai"; then
3440
fn_print_dots "Checking installer"
3541
fn_print_checking_eol
3642
fn_script_log_info "Checking installer: CHECKING"
@@ -42,7 +48,7 @@ fn_monitor_check_install() {
4248
}
4349

4450
fn_monitor_check_debug() {
45-
if [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} debug")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} d")" != "0" ]; then
51+
if fn_monitor_command_running "debug|d"; then
4652
fn_print_dots "Checking debug"
4753
fn_print_checking_eol
4854
fn_print_info "Checking debug: Debug is running"
@@ -53,7 +59,7 @@ fn_monitor_check_debug() {
5359
}
5460

5561
fn_monitor_check_details() {
56-
if [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} details")" != "0" ] || [ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} dt")" != "0" ]; then
62+
if fn_monitor_command_running "details|dt"; then
5763
fn_print_dots "Checking details"
5864
fn_print_checking_eol
5965
fn_print_info "Checking details: Details is running"
@@ -76,7 +82,7 @@ fn_monitor_check_starting() {
7682
fi
7783
fi
7884

79-
if [ -f "${lockdir}/${selfname}-starting.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} start")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} st")" != "0" ]]; then
85+
if [ -f "${lockdir}/${selfname}-starting.lock" ] && fn_monitor_command_running "start|st"; then
8086
fn_print_dots "Checking start"
8187
fn_print_checking_eol
8288
fn_print_info "Checking start: LinuxGSM is starting"
@@ -99,7 +105,7 @@ fn_monitor_check_stopping() {
99105
fi
100106
fi
101107

102-
if [ -f "${lockdir}/${selfname}-stopping.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} stop")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} sp")" != "0" ]]; then
108+
if [ -f "${lockdir}/${selfname}-stopping.lock" ] && fn_monitor_command_running "stop|sp"; then
103109
fn_print_dots "Checking stop"
104110
fn_print_checking_eol
105111
fn_print_info "Checking stop: LinuxGSM is stopping"
@@ -142,7 +148,7 @@ fn_monitor_check_backup() {
142148
fi
143149
fi
144150

145-
if [ -f "${lockdir}/backup.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} backup")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} b")" != "0" ]]; then
151+
if [ -f "${lockdir}/backup.lock" ] && fn_monitor_command_running "backup|b"; then
146152
fn_print_dots "Checking backup"
147153
fn_print_checking_eol
148154
fn_print_info "Checking backup: Backup is running"
@@ -165,7 +171,7 @@ fn_monitor_check_update() {
165171
fi
166172
fi
167173

168-
if [ -f "${lockdir}/update.lock" ] && [[ "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} update")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} u")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} validate")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} v")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} force-update")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} fu")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} update-restart")" != "0" || "$(pgrep -fcx -u "${USER}" "/bin/bash ./${selfname} ur")" != "0" ]]; then
174+
if [ -f "${lockdir}/update.lock" ] && fn_monitor_command_running "update|u|validate|v|force-update|fu|update-restart|ur"; then
169175
fn_print_dots "Checking update"
170176
fn_print_checking_eol
171177
fn_print_info "Checking update: LinuxGSM is updating the game server"

0 commit comments

Comments
 (0)