From 048f4880a1d94f3e96af56e74473d7016ea044ea Mon Sep 17 00:00:00 2001 From: Marc Herbert Date: Thu, 3 Dec 2020 22:09:28 -0800 Subject: [PATCH] exit handler: don't use a subshell to list children still running Use a new children_left.txt log file instead. Using a subshell forced us to filter it out with grep -v $SCRIPT_NAME which is more complicated, incompatible with exec wrappers like multiple-pipeline-capture/playback.sh and incompatible with running concurrent instances. Signed-off-by: Marc Herbert --- case-lib/hijack.sh | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/case-lib/hijack.sh b/case-lib/hijack.sh index bd5e12d2..59b455fe 100644 --- a/case-lib/hijack.sh +++ b/case-lib/hijack.sh @@ -148,13 +148,10 @@ function func_exit_handler() # get ps command result as list local -a cmd_lst - # $$ as current script pid - # NOTICE: already test with $BASHPID: - # it can output the same result of $$ - # but the result could not be stored in the array - readarray -t cmd_lst < <(pgrep -P $$ -a|grep -v "$SCRIPT_NAME") + # can't run pgrep in any subshell because the latter would pollute the list + if pgrep -P $$ -a > "$LOG_ROOT/children_left.txt"; then + readarray -t cmd_lst < "$LOG_ROOT/children_left.txt" # now force kill target process which maybe block the script quit - if [ ${#cmd_lst[@]} -gt 0 ]; then local line dlogw "Process(es) started by $SCRIPT_NAME are still active, killing these process(es):" for line in "${cmd_lst[@]}" @@ -163,6 +160,8 @@ function func_exit_handler() dlogw "Kill cmd:'${line#* }' by kill -9" kill -9 "${line%% *}" done + else + rm "$LOG_ROOT/children_left.txt" fi # check if function already defined.