How to kill several child processes spawned from same parent?
For testing purpose I am running several (few 100s
) expect
scripts. All are spawned from the same parent (sometimes its pid is 1
). Is there any way to kill them at one stretch without changing their source code and without killing the parent process itself ? Something like:
[root@devx-csb4 expect_scripts] kill -9 <child of pid=...>
All expect script are just the same but ran in a loop; see the following ps -ef | grep milind
root 19879 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13265 55650
root 19889 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13266 55660
root 19899 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13267 55670
root 19930 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13268 55680
root 19940 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13269 55690
root 19973 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13270 55700
root 19983 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13271 55710
root 19993 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13272 55720
root 20024 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13273 55730
root 20034 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13274 55740
root 20067 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13275 55750
root 20104 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13276 55760
root 20114 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13277 55770
root 20145 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13278 55780
root 20155 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13279 55790
root 20188开发者_C百科 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13280 55800
root 20198 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13281 55810
root 20208 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13282 55820
root 20239 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13283 55830
root 20249 1 0 20:27 pts/9 00:00:00 /usr/bin/expect /root/pjproject-1.0.3/pjsip-apps/bin/expect_scripts/milind.exp 13284 55840
Give them all a name with a common substring and use pkill(1)
Use ps, get the parent process ID, and kill all the PIDs attached to the parent process. This is undesirable when the parent process is 1. Notice that your notion of using
kill -9 pid=
suffers from the same problem -- all the system daemon processes are children of PID 1.Keep a list of the expect script names; run ps and for each name in your list, find the PID; kill it.
精彩评论