@@ -4365,45 +4365,44 @@ get_pids_by_name() {
43654365 done
43664366}
43674367
4368- # Backward-compatible wrapper:
4369- # get_one_pid_by_name <name> -> first PID
4370- # get_one_pid_by_name <name> all -> all PIDs (newline-separated)
4368+ # get_one_pid_by_name <name> [all]
4369+ #
4370+ # Without 'all': prints the lowest (oldest) matching PID, returns 1 if none found.
4371+ # With 'all': prints all matching PIDs space-separated, returns 1 if none found.
4372+ #
4373+ # Note: /proc/<pid>/comm is truncated to 15 chars by the kernel.
43714374get_one_pid_by_name () {
4372- name=" $1 "
4373- mode=" ${2:- } "
4374-
4375- pids=" "
4376- first_pid=" "
4377-
4378- for d in /proc/[0-9]* ; do
4379- [ -r " $d /comm" ] || continue
4380- comm=$( tr -d ' \r\n' < " $d /comm" 2> /dev/null)
4381- [ " $comm " = " $name " ] || continue
4382-
4383- pid=${d#/ proc/ }
4384- case " $pid " in
4385- ' ' |* [!0-9]* )
4386- continue
4387- ;;
4388- esac
4389-
4390- if [ -z " $first_pid " ] || [ " $pid " -lt " $first_pid " ]; then
4391- first_pid=" $pid "
4375+ gopn_name=" $1 "
4376+ gopn_mode=" ${2:- } "
4377+ gopn_pids=" "
4378+ gopn_first_pid=" "
4379+
4380+ for gopn_d in /proc/[0-9]* ; do
4381+ [ -r " $gopn_d /comm" ] || continue
4382+ gopn_comm=$( tr -d ' \r\n' < " $gopn_d /comm" 2> /dev/null)
4383+ [ " $gopn_comm " = " $gopn_name " ] || continue
4384+
4385+ gopn_pid=${gopn_d#/ proc/ }
4386+ gopn_pid=$( sanitize_pid " $gopn_pid " )
4387+ [ -n " $gopn_pid " ] || continue
4388+
4389+ if [ -z " $gopn_first_pid " ] || [ " $gopn_pid " -lt " $gopn_first_pid " ]; then
4390+ gopn_first_pid=" $gopn_pid "
43924391 fi
43934392
4394- if [ -z " $pids " ]; then
4395- pids =" $pid "
4393+ if [ -z " $gopn_pids " ]; then
4394+ gopn_pids =" $gopn_pid "
43964395 else
4397- pids =" $pids $pid "
4396+ gopn_pids =" $gopn_pids $gopn_pid "
43984397 fi
43994398 done
44004399
4401- [ -n " $pids " ] || return 1
4400+ [ -n " $gopn_pids " ] || return 1
44024401
4403- if [ " $mode " = " all" ]; then
4404- printf ' %s\n' " $pids "
4402+ if [ " $gopn_mode " = " all" ]; then
4403+ printf ' %s\n' " $gopn_pids "
44054404 else
4406- printf ' %s\n' " $first_pid "
4405+ printf ' %s\n' " $gopn_first_pid "
44074406 fi
44084407}
44094408
@@ -4474,46 +4473,58 @@ kill_process() {
44744473 return 0
44754474}
44764475
4476+ # is_process_running <name>
4477+ #
4478+ # Checks whether a process with the given <name> is currently running.
4479+ #
4480+ # Behavior:
4481+ # - Returns 0 if at least one matching process exists; returns 1 otherwise.
4482+ # - Logs a clear status line:
4483+ # "Process '<name>' is running." OR "Process '<name>' is not running."
4484+ # - Uses get_one_pid_by_name "<name>" all to gather *all* matching PIDs.
4485+ # - If multiple instances exist, logs the PID list as a summary.
44774486is_process_running () {
4478- name =" $1 "
4487+ ipr_name =" $1 "
44794488
4480- pids =$( get_one_pid_by_name " $name " all 2> /dev/null) || {
4481- log_info " Process '$name ' is not running."
4489+ ipr_pids =$( get_one_pid_by_name " $ipr_name " all 2> /dev/null) || {
4490+ log_info " Process '$ipr_name ' is not running."
44824491 return 1
44834492 }
44844493
4485- log_info " Process '$name ' is running."
4494+ log_info " Process '$ipr_name ' is running."
44864495
4487- # Only add extra debug if multiple instances exist
4488- case " $pids " in
4496+ # Extra summary only if multiple instances exist
4497+ case " $ipr_pids " in
44894498 * " " * )
4490- log_info " Process '$name ' instances: $pids "
4491- ;;
4492- * )
4493- return 0
4499+ log_info " Process '$ipr_name ' instances: $ipr_pids "
44944500 ;;
44954501 esac
44964502
4497- for pid in $pids ; do
4498- case " $pid " in
4499- ' ' |* [!0-9]* )
4500- continue
4501- ;;
4502- esac
4503+ # Always print PID -> cmdline/comm for CI debug (single or multiple)
4504+ # ShellCheck-safe iteration: split spaces to newlines.
4505+ printf ' %s\n' " $ipr_pids " | tr ' ' ' \n' | while IFS= read -r ipr_pid; do
4506+ ipr_pid=$( sanitize_pid " $ipr_pid " )
4507+ [ -n " $ipr_pid " ] || continue
4508+
4509+ # PID may have exited between collection and inspection
4510+ if [ ! -d " /proc/$ipr_pid " ]; then
4511+ log_info " Process '$ipr_name ' PID $ipr_pid is no longer present in /proc"
4512+ continue
4513+ fi
45034514
4504- cmd =" "
4505- if [ -r " /proc/$pid /cmdline" ]; then
4506- cmd =$( tr ' \000' ' ' < " /proc/$pid /cmdline" 2> /dev/null)
4515+ ipr_cmd =" "
4516+ if [ -r " /proc/$ipr_pid /cmdline" ]; then
4517+ ipr_cmd =$( tr ' \000' ' ' < " /proc/$ipr_pid /cmdline" 2> /dev/null)
45074518 fi
45084519
4509- if [ -n " $cmd " ]; then
4510- log_info " Process '$name ' PID $pid cmdline: $cmd "
4520+ if [ -n " $ipr_cmd " ]; then
4521+ log_info " Process '$ipr_name ' PID $ipr_pid cmdline: $ipr_cmd "
45114522 else
4512- comm =" "
4513- if [ -r " /proc/$pid /comm" ]; then
4514- comm =$( tr -d ' \r\n' < " /proc/$pid /comm" 2> /dev/null)
4523+ ipr_comm =" "
4524+ if [ -r " /proc/$ipr_pid /comm" ]; then
4525+ ipr_comm =$( tr -d ' \r\n' < " /proc/$ipr_pid /comm" 2> /dev/null)
45154526 fi
4516- log_info " Process '$name ' PID $pid comm: ${comm :- unknown} "
4527+ log_info " Process '$ipr_name ' PID $ipr_pid comm: ${ipr_comm :- unknown} "
45174528 fi
45184529 done
45194530
0 commit comments