Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce usage of SCRIPT_NAME #546

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked pgrep -P $$ -a it will show the script it self, how do you remove it from the list

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong, demo:

hostname:~$ pgrep -P $$ -a
hostname:~$ sleep 1000 &
[1] 140297
hostname:~$ 
hostname:~$ ps f
    PID TTY      STAT   TIME COMMAND
 140238 pts/3    Ss     0:00 -bash
 140297 pts/3    S      0:00  \_ sleep 1000
 140298 pts/3    R+     0:00  \_ ps f
 139197 pts/2    Ss+    0:00 -bash
 138772 pts/1    Ss+    0:00 /bin/sh -i
hostname:~$ echo $$
140238
hostname:~$ pgrep -P $$ -a
140297 sleep 1000

# can't run pgrep in any subshell because the latter would pollute the list
if pgrep -P $$ -a > "$LOG_ROOT/children_left.txt"; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see any need to use a tmp file here, this will bring more trouble with sub-test
why not use readarray -t cmd_lst < <(pgrep -P $$ -a)

And the whole logic is changed to me, the parent will still be killed by itself now.

How is # can't run pgrep in any subshell because the latter would pollute the list take effect here

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
2 changes: 1 addition & 1 deletion case-lib/opt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func_opt_parse_option()
# removing the leading '##' or '## ' from documentation.
_dump_case_description()
{
grep '^##' "$SCRIPT_NAME" | sed 's/^##//g' | sed 's/^ //g'
grep '^##' "$0" | sed 's/^##//g' | sed 's/^ //g'
}

# This function helps to fill below four variables
Expand Down
Loading