如何杀死从同一个父级产生的几个子进程?

时间:2011-06-10 14:49:32

标签: unix process kill

出于测试目的,我运行了几个(100s} expect个脚本。所有这些都是从同一个父级生成的(有时它的pid是1)。有没有办法在不改变源代码的情况下一次性杀死它们而而不会破坏父进程本身?类似的东西:

[root@devx-csb4 expect_scripts] kill -9 <child of pid=...>

所有期望脚本都是一样的但是循环运行;请参阅以下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     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

1 个答案:

答案 0 :(得分:2)

  1. 使用公共子字符串为所有人命名并使用 pkill(1)

  2. 使用ps,获取父进程ID,并终止附加到父进程的所有PID。当父进程为1时,这是不可取的。请注意,使用kill -9 pid=的概念遇到同样的问题 - 所有系统守护程序进程都是PID 1的子进程。

  3. 保留期望脚本名称的列表;运行ps并为列表中的每个名称找到PID;杀了它。