@@ -602,7 +602,7 @@ extract_tar_from_url() {
602602 log_info " Extracting $( basename " $tarfile " ) ..."
603603 if tar -xvf " $tarfile " ; then
604604 : > " $markfile " 2> /dev/null || true
605- # Clear the minimal/offline sentinel only if it exists (SC2015-safe)
605+ # Clear the minimal/offline sentinel only if it exists (SC2015-safe)
606606 if [ -f " $skip_sentinel " ]; then
607607 rm -f " $skip_sentinel " 2> /dev/null || true
608608 fi
@@ -706,7 +706,7 @@ weston_stop() {
706706 log_info " Stopping Weston..."
707707 pkill -x weston
708708 for i in $( seq 1 10) ; do
709- log_info " Waiting for Weston to stop with $i attempt "
709+ log_info " Waiting for Weston to stop with $i attempt "
710710 if ! weston_is_running; then
711711 log_info " Weston stopped successfully"
712712 return 0
@@ -3846,3 +3846,95 @@ ensure_network_online() {
38463846 unset net_script_path net_ifaces net_wifi net_ifc net_rc net_had_any_ip
38473847 return 1
38483848}
3849+
3850+ kill_process () {
3851+ PID=" $1 "
3852+ KILL_TERM_GRACE=" ${KILL_TERM_GRACE:- 5} "
3853+ KILL_KILL_GRACE=" ${KILL_KILL_GRACE:- 5} "
3854+ SELF_PID=" $$ "
3855+
3856+ # Safety checks
3857+ if [ " $PID " -eq 1 ] || [ " $PID " -eq " $SELF_PID " ]; then
3858+ log_warn " Refusing to kill PID $PID (init or self)"
3859+ return 1
3860+ fi
3861+
3862+ # Check if process exists
3863+ if ! kill -0 " $PID " 2> /dev/null; then
3864+ log_info " Process $PID not running"
3865+ return 0
3866+ fi
3867+
3868+ log_info " Sending SIGTERM to PID $PID "
3869+ kill -TERM " $PID " 2> /dev/null
3870+ sleep " $KILL_TERM_GRACE "
3871+
3872+ if kill -0 " $PID " 2> /dev/null; then
3873+ log_info " Sending SIGKILL to PID $PID "
3874+ kill -KILL " $PID " 2> /dev/null
3875+ sleep " $KILL_KILL_GRACE "
3876+ fi
3877+
3878+ # Final check
3879+ if kill -0 " $PID " 2> /dev/null; then
3880+ log_warn " Failed to kill process $PID "
3881+ return 1
3882+ else
3883+ log_info " Process $PID terminated successfully"
3884+ return 0
3885+ fi
3886+ }
3887+
3888+ is_process_running () {
3889+ if [ -z " $1 " ]; then
3890+ log_info " Usage: is_running <process_name_or_pid>"
3891+ return 1
3892+ fi
3893+ input=" $1 "
3894+ case " $input " in
3895+ ' ' |* [!0-9]* )
3896+ # Non-numeric input: treat as process name
3897+ if ps -e | grep -w " $input " > /dev/null 2>&1 ||
3898+ ps -A | grep -w " $input " > /dev/null 2>&1 ; then
3899+ log_info " Process '$input ' is running."
3900+ return 0
3901+ else
3902+ log_info " Process '$input ' is not running."
3903+ return 1
3904+ fi
3905+ ;;
3906+ * )
3907+ # Numeric input: treat as PID
3908+ if kill -0 " $input " 2> /dev/null; then
3909+ log_info " Process with PID $input is running."
3910+ return 0
3911+ else
3912+ log_info " Process with PID $input is not running."
3913+ return 1
3914+ fi
3915+ ;;
3916+ esac
3917+ }
3918+
3919+ get_pid () {
3920+ if [ -z " $1 " ]; then
3921+ log_info " Usage: get_pid <process_name>"
3922+ return 1
3923+ fi
3924+
3925+ process_name=" $1 "
3926+
3927+ # Try multiple ps variants for compatibility
3928+ pid=$( ps -e | awk -v name=" $process_name " ' $NF == name { print $1 }' )
3929+ [ -z " $pid " ] && pid=$( ps -A | awk -v name=" $process_name " ' $NF == name { print $1 }' )
3930+ [ -z " $pid " ] && pid=$( ps -aux | awk -v name=" $process_name " ' $11 == name { print $2 }' )
3931+ [ -z " $pid " ] && pid=$( ps -ef | awk -v name=" $process_name " ' $NF == name { print $2 }' )
3932+
3933+ if [ -n " $pid " ]; then
3934+ echo " $pid "
3935+ return 0
3936+ else
3937+ log_info " Process '$process_name ' not found."
3938+ return 1
3939+ fi
3940+ }
0 commit comments