I have simple bash script:
( sleep 5; echo 'A'; sleep 100 ) &
( sleep 7; echo 'B'; sleep 100 ) &
I want to kill this two process after second process print 'B' and of couse first process print 'A'. ( May be not echo - may be other label after first sleep开发者_运维百科. ) How to do it ?
May be like this
( sleep 5; echo 'A'; sleep 100 ) &
export Last=$!
( sleep 7; echo 'B'; kill-9 $Last; sleep 100 ) &
Try this:
jobs -p | xargs kill
jobs -p
prints the process IDs of all background jobs from this shell window.
xargs
passes what it reads on stdin
to the program you provide.
kill
sends SIGTERM
to each process ID you give it.
Note, though, that this will kill ALL background jobs started from this shell, not just the last two.
If you know you want to kill just the most recently started X jobs, use this:
jobs -p | tail -n [number of jobs] | xargs kill
精彩评论