@@ -4343,34 +4343,68 @@ sanitize_pid() {
43434343 | awk ' {print $1; exit}'
43444344}
43454345
4346- # Get a single PID (first match) for a given process name.
4347- # Prefer pgrep, then get_pid, then a ps fallback.
4348- get_one_pid_by_name () {
4346+ get_pids_by_name () {
43494347 name=" $1 "
4350- [ -z " $name " ] && return 1
4351-
4352- pid=" "
4353-
4354- # Prefer pgrep
4355- if command -v pgrep > /dev/null 2>&1 ; then
4356- pid=$( pgrep -x " $name " 2> /dev/null | awk ' NR==1{print; exit}' )
4357- pid=$( sanitize_pid " $pid " )
4358- [ -n " $pid " ] && { printf ' %s\n' " $pid " ; return 0; }
4348+
4349+ if [ -z " $name " ]; then
4350+ return 1
43594351 fi
4360-
4361- # Fallback to get_pid (should already return first PID after your update)
4362- if command -v get_pid > /dev/null 2>&1 ; then
4363- pid=$( get_pid " $name " 2> /dev/null)
4364- pid=$( sanitize_pid " $pid " )
4365- [ -n " $pid " ] && { printf ' %s\n' " $pid " ; return 0; }
4352+
4353+ # Print one PID per line (sanitized), for all processes whose basename matches $name
4354+ ps -ef 2> /dev/null | awk -v n=" $name " '
4355+ NR==1 { next }
4356+ {
4357+ cmd=$8
4358+ sub(".*/", "", cmd)
4359+ if (cmd == n) { print $2 }
4360+ }' | while IFS= read -r p; do
4361+ p_clean=$( sanitize_pid " $p " )
4362+ if [ -n " $p_clean " ]; then
4363+ printf ' %s\n' " $p_clean "
4364+ fi
4365+ done
4366+ }
4367+
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)
4371+ get_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 "
4392+ fi
4393+
4394+ if [ -z " $pids " ]; then
4395+ pids=" $pid "
4396+ else
4397+ pids=" $pids $pid "
4398+ fi
4399+ done
4400+
4401+ [ -n " $pids " ] || return 1
4402+
4403+ if [ " $mode " = " all" ]; then
4404+ printf ' %s\n' " $pids "
4405+ else
4406+ printf ' %s\n' " $first_pid "
43664407 fi
4367-
4368- # Final fallback: ps
4369- pid=$( ps -e 2> /dev/null | awk -v n=" $name " ' $NF==n {print $1; exit}' )
4370- pid=$( sanitize_pid " $pid " )
4371- [ -n " $pid " ] && { printf ' %s\n' " $pid " ; return 0; }
4372-
4373- return 1
43744408}
43754409
43764410# Wait until PID is alive (kill -0 succeeds) or timeout seconds elapse.
@@ -4441,52 +4475,49 @@ kill_process() {
44414475}
44424476
44434477is_process_running () {
4444- if [ -z " $1 " ]; then
4445- log_info " Usage: is_running <process_name_or_pid>"
4446- return 1
4447- fi
4478+ name=" $1 "
44484479
4449- input=" $1 "
4450- case " $input " in
4451- ' ' |* [!0-9]* )
4452- # Non-numeric input: treat as process name
4453- found=0
4480+ pids=$( get_one_pid_by_name " $name " all 2> /dev/null) || {
4481+ log_info " Process '$name ' is not running."
4482+ return 1
4483+ }
44544484
4455- # Prefer pgrep if available (ShellCheck-friendly, efficient)
4456- if command -v pgrep > /dev/null 2>&1 ; then
4457- if pgrep -x " $input " > /dev/null 2>&1 ; then
4458- found=1
4459- fi
4460- else
4461- # POSIX fallback: avoid 'ps | grep' to silence SC2009
4462- # Match as a separate word to mimic 'grep -w'
4463- if ps -e 2> /dev/null | awk -v name=" $input " '
4464- $0 ~ ("(^|[[:space:]])" name "([[:space:]]|$)") { exit 0 }
4465- END { exit 1 }
4466- ' ; then
4467- found=1
4468- fi
4469- fi
4485+ log_info " Process '$name ' is running."
44704486
4471- if [ " $found " -eq 1 ]; then
4472- log_info " Process '$input ' is running."
4487+ # Only add extra debug if multiple instances exist
4488+ case " $pids " in
4489+ * " " * )
4490+ log_info " Process '$name ' instances: $pids "
4491+ ;;
4492+ * )
44734493 return 0
4474- else
4475- log_info " Process '$input ' is not running."
4476- return 1
4494+ ;;
4495+ esac
4496+
4497+ for pid in $pids ; do
4498+ case " $pid " in
4499+ ' ' |* [!0-9]* )
4500+ continue
4501+ ;;
4502+ esac
4503+
4504+ cmd=" "
4505+ if [ -r " /proc/$pid /cmdline" ]; then
4506+ cmd=$( tr ' \000' ' ' < " /proc/$pid /cmdline" 2> /dev/null)
44774507 fi
4478- ;;
4479- * )
4480- # Numeric input: treat as PID
4481- if kill -0 " $input " 2> /dev/null; then
4482- log_info " Process with PID $input is running."
4483- return 0
4508+
4509+ if [ -n " $cmd " ]; then
4510+ log_info " Process '$name ' PID $pid cmdline: $cmd "
44844511 else
4485- log_info " Process with PID $input is not running."
4486- return 1
4512+ comm=" "
4513+ if [ -r " /proc/$pid /comm" ]; then
4514+ comm=$( tr -d ' \r\n' < " /proc/$pid /comm" 2> /dev/null)
4515+ fi
4516+ log_info " Process '$name ' PID $pid comm: ${comm:- unknown} "
44874517 fi
4488- ;;
4489- esac
4518+ done
4519+
4520+ return 0
44904521}
44914522
44924523get_pid () {
0 commit comments