List the group process by ps "-o" custom format command
Assume you are running a bash script which invokes lots of
sh child-threads e.g. "sh sleep 1234567"
%p == process id
%r == group process id
%c == command line (e.g. "sh" here)
%a == command line argument (e.g. "sleep 1234567" here)
代碼:
$ ps xf -o "%p %r %c %a" | grep 1234567 | grep sleep
11966 11833 sh sleep 1234567
11976 11833 sh sleep 1234567
11986 11833 sh sleep 1234567
11996 11833 sh sleep 1234567
12106 11833 sh sleep 1234567
Then, the 2nd colume "11833" is the group process"
You can just use kill -11833 (with single dash before group id)
to kill the all five child sleep process/threads.
Combine group-thread kill -group_process_id with xargs
代碼:
$ ps xf -o "%p %r %c %a" | grep 1234567 | grep sleep | tail -1 | awk '{print $2}' | xargs -Igid kill -- -gid
Note: The double dash is needed here for xargs + kill -gid
|