Closed ale5000-git closed 1 year ago
I have noticed this doesn't work:
pgrep -a -s 0
Have I done something worng?
Use a negative pid to kill all children or use the job id:
~ $ sh -c 'sleep 10' &
[1] 2080
~ $ kill -- -2080
[1]+ Terminated sh -c "sleep 10"
~ $ sh -c 'sleep 10' &
[1] 3600
~ $ kill %1
[1]+ Terminated sh -c "sleep 10"
~ $
pgrep -a -s 0
Have I done something worng?
No, it's just that session ids aren't supported on Windows.
I need to kill all descendants but not the main script. I'm not expert about this but where do I get the negative pid or the job id? It isn't interactive, and I cannot use $! since it is executed under a subshell.
Example of what I need:
( sleep 100 && echo doh & )
( sleep 100 && echo doh & )
( sleep 100 && echo doh & )
kill all (except the main script)
If it isn't interactive you won't have a job id. Use ps
to get the pid? I don't know.
This list descendants but it seems not the ones inside the subshell:
_list_descendants()
{
local _children="$(pgrep -P "${1?}")"
for _pid in ${_children?}
do
printf '%s ' "${_pid?}"
_list_descendants "${_pid?}"
done
}
_list_descendants "${$}"
You need the pids of the background jobs. They're children of the subshells, not the current process.
( sleep 100 && echo doh & echo $! >pids )
( sleep 100 && echo doh & echo $! >>pids )
( sleep 100 && echo doh & echo $! >>pids )
( sleep 100 && echo doh & echo $! >>pids )
cat pids
It works, thanks :)
Hi, this is just a question.
For this simple case:
sleep 20 && echo doh &
it is enough to do this:pkill -KILL -P "${$}"
But for more complicate cases like this:
How to kill it?
I have found this:
kill $(ps -s $$ -o pid=)
and this:but they do not work due to limitations in the BusyBox version of them.
Any suggestion? Thanks in advance.