Skip to content

Commit

Permalink
exit handler: don't use a subshell to list children still running
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
marc-hb committed Jul 30, 2024
1 parent 9bc74c8 commit 048f488
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions case-lib/hijack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
Expand All @@ -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.
Expand Down

0 comments on commit 048f488

Please sign in to comment.