From 1aee330230ff557f281a7a81888ea9724b479f74 Mon Sep 17 00:00:00 2001 From: Thomas Oettli Date: Mon, 4 Nov 2024 10:40:50 +0100 Subject: [PATCH] improve sub-proc handling / killing --- snmpd-oid-daemon.sh | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/snmpd-oid-daemon.sh b/snmpd-oid-daemon.sh index f158c10..84604ff 100755 --- a/snmpd-oid-daemon.sh +++ b/snmpd-oid-daemon.sh @@ -598,24 +598,30 @@ while :; do read -t 0 -u $fd (( $? == 0 )) || continue fi + data=$(timeout 1 cat <&$fd) rc=$? eval "exec $fd>&-" fdtable[$func]=-1 + pid=${pidtable[$func]} if (( rc == 124 )); then - echo "gather: timeout receiving data from $func (PID $pid, FD $fd), killing process" >&2 + echo "gather: timeout receiving data from $func (PID $pid, FD $fd), sending SIGTERM" >&2 kill -SIGTERM $pid sleep 1 - ps -p $pid >/dev/null && kill -SIGKILL $pid - rc=137 - else - wait $pid &>/dev/null - rc=$? - # the wait function in older Bash versions prior to 5.1 always returns 127 if - # the sub-process already exited at this point - (( rc == 127 )) && rc=0 + if ps -p $pid >/dev/null; then + echo "gather: unable to terminate $func (PID $pid, FD $fd), sending SIGKILL" >&2 + kill -SIGKILL $pid + fi + continue fi + + wait $pid &>/dev/null + rc=$? + # the wait function in older Bash versions prior to 5.1 always returns 127 if the + # sub-process already exited at this point + (( rc == 127 )) && rc=0 + echo "gather: $func (PID $pid, FD $fd) exited with rc = $rc" >&$DEBUGLOG if (( rc == 0 )) && [ -n "$data" ]; then echo "gather: sending data to cache" >&$DEBUGLOG