@@ -4334,42 +4334,110 @@ ensure_network_online() {
43344334 return 1
43354335}
43364336
4337+ # Return the first numeric token found in input (helps when tools return multi-PID / noisy output)
4338+ sanitize_pid () {
4339+ # Return first numeric token from input (single PID) or empty
4340+ printf ' %s' " $1 " \
4341+ | tr -d ' \r' \
4342+ | tr -c ' 0-9 \n' ' ' \
4343+ | awk ' {print $1; exit}'
4344+ }
4345+
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 () {
4349+ 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; }
4359+ 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; }
4366+ 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
4374+ }
4375+
4376+ # Wait until PID is alive (kill -0 succeeds) or timeout seconds elapse.
4377+ wait_pid_alive () {
4378+ pid=" $1 "
4379+ timeout=" ${2:- 10} "
4380+
4381+ pid=$( sanitize_pid " $pid " )
4382+ case " $pid " in
4383+ ' ' |* [!0-9]* )
4384+ return 1
4385+ ;;
4386+ esac
4387+
4388+ i=0
4389+ while [ " $i " -lt " $timeout " ]; do
4390+ if kill -0 " $pid " 2> /dev/null; then
4391+ return 0
4392+ fi
4393+ sleep 1
4394+ i=$(( i + 1 ))
4395+ done
4396+ return 1
4397+ }
4398+
4399+ # Kill process for the given PID
43374400kill_process () {
43384401 PID=" $1 "
43394402 KILL_TERM_GRACE=" ${KILL_TERM_GRACE:- 5} "
43404403 KILL_KILL_GRACE=" ${KILL_KILL_GRACE:- 5} "
43414404 SELF_PID=" $$ "
4342-
4405+
4406+ case " $PID " in
4407+ ' ' |* [!0-9]* )
4408+ log_warn " Refusing to kill non-numeric PID '$PID '"
4409+ return 1
4410+ ;;
4411+ esac
4412+
43434413 # Safety checks
43444414 if [ " $PID " -eq 1 ] || [ " $PID " -eq " $SELF_PID " ]; then
43454415 log_warn " Refusing to kill PID $PID (init or self)"
43464416 return 1
43474417 fi
4348-
4349- # Check if process exists
4418+
43504419 if ! kill -0 " $PID " 2> /dev/null; then
43514420 log_info " Process $PID not running"
43524421 return 0
43534422 fi
4354-
4423+
43554424 log_info " Sending SIGTERM to PID $PID "
43564425 kill -TERM " $PID " 2> /dev/null
43574426 sleep " $KILL_TERM_GRACE "
4358-
4427+
43594428 if kill -0 " $PID " 2> /dev/null; then
43604429 log_info " Sending SIGKILL to PID $PID "
43614430 kill -KILL " $PID " 2> /dev/null
43624431 sleep " $KILL_KILL_GRACE "
43634432 fi
4364-
4365- # Final check
4433+
43664434 if kill -0 " $PID " 2> /dev/null; then
43674435 log_warn " Failed to kill process $PID "
43684436 return 1
4369- else
4370- log_info " Process $PID terminated successfully"
4371- return 0
43724437 fi
4438+
4439+ log_info " Process $PID terminated successfully"
4440+ return 0
43734441}
43744442
43754443is_process_running () {
@@ -4426,20 +4494,25 @@ get_pid() {
44264494 log_info " Usage: get_pid <process_name>"
44274495 return 1
44284496 fi
4429-
4497+
44304498 process_name=" $1 "
4431-
4432- # Try multiple ps variants for compatibility
4433- pid=$( ps -e | awk -v name=" $process_name " ' $NF == name { print $1 }' )
4434- [ -z " $pid " ] && pid=$( ps -A | awk -v name=" $process_name " ' $NF == name { print $1 }' )
4435- [ -z " $pid " ] && pid=$( ps -aux | awk -v name=" $process_name " ' $11 == name { print $2 }' )
4436- [ -z " $pid " ] && pid=$( ps -ef | awk -v name=" $process_name " ' $NF == name { print $2 }' )
4437-
4499+ pid=" "
4500+
4501+ # Prefer pgrep if available (fast + clean)
4502+ if command -v pgrep > /dev/null 2>&1 ; then
4503+ pid=$( pgrep -x " $process_name " 2> /dev/null | awk ' NR==1 {print; exit}' )
4504+ fi
4505+
4506+ # POSIX fallback: ps
4507+ if [ -z " $pid " ]; then
4508+ pid=$( ps -e 2> /dev/null | awk -v name=" $process_name " ' $NF == name { print $1; exit }' )
4509+ fi
4510+
44384511 if [ -n " $pid " ]; then
4439- echo " $pid "
4512+ printf ' %s\n ' " $pid "
44404513 return 0
4441- else
4442- log_info " Process '$process_name ' not found."
4443- return 1
44444514 fi
4515+
4516+ log_info " Process '$process_name ' not found."
4517+ return 1
44454518}
0 commit comments