ChinaUnix首页 > 精华文章 > Perl > 正文

[精彩] 怎样写一个脚本来判断那些进程已经死去,并KILL掉?


http://www.chinaunix.net 作者:clic  发表于:2009-06-25 12:38:50
发表评论】 【查看原文】 【Perl讨论区】【关闭

怎样来写脚本来判断和杀进程呢?



 apile 回复于:2003-05-26 15:54:46


$username =`whoami`;
chomp($username);
@aa = `ps -ef|grep $username|grep -v grep |grep -v ps|cut -c8-14`;
for(@aa){
  chomp;
  next unless /^\d+$/;
  unless(  kill 0 =>;$_){
      kill 9 =>; $_;
  }
}

-c8-14可能隨著系統不同..
而且上面我修改後的好處是只會刪除自己的process id
不會跟其他人的有關係..
另外如果是root要做..把grep $username拿掉就可以了..

因為kill -0 $pid只對..自己的process有用..其他人的沒用..


 deathcult 回复于:2003-05-26 18:33:22

ps aux,ps -ef(各种平台参数有些不同)
然后找stat字段,Z表示僵死状态。

打印僵死进程的pid。
ps -ef|awk '{if($3=="Z")print $1}'


 deathcult 回复于:2003-05-26 18:34:32

ps -ef|awk '{if($3=="Z")print "kill -9 "$1}'|sh

小心使用!可以先把|sh去掉,看看打印结果。


 apile 回复于:2003-05-26 18:50:41

kill -0 $pid
測試process是不是還有在processing...
true..有
false..沒有

ps -ef 應該大多數OS..都有支援..


 clic 回复于:2003-05-26 19:35:18

看样子要找些书看看才行呀,哪里有没有PERL的书籍可下载


 deathcult 回复于:2003-05-27 10:40:32

这些是shell的知识,先看看shell吧, :)


 yoursmile 回复于:2003-12-08 15:30:23

引用:原帖由 "apile" 发表:

$username =`whoami`;
chomp($username);
@aa = `ps -ef|grep $username|grep -v grep |grep -v ps|cut -c8-14`;
for(@aa){
  chomp;
  next unless /^\d+$/;
  unless(  kill 0 =>;$_){
      kill 9 ..........



我收!!~~

 :em02:


 jason_liuk 回复于:2007-03-26 11:13:01

阿里嘎多,收了


 rxpmcb 回复于:2007-03-26 14:10:51


#!/usr/bin/perl

foreach (@ARGV) {
        $process{$_} = 0;
}
open STATUS, "ps -e |";
while (<STATUS>) {
        $_ =~ s/^\s+//;
        print "$_\n";
        ($pid, $name) = (split /\s+/)[0,3];
        if (defined($process{$name})) {
                print "kill process $name, process id $pid ...\n";
                $cnt = kill 9, $pid;
                printf STDERR "$pid: $!\n" if !$cnt;
        }
}
close STATUS;




 supercase 回复于:2008-01-17 22:33:03

up


 gaochong 回复于:2009-06-24 21:46:16

尽量不要用grep 和 cut 吧。


请看看下边这个url,建议看完。

http://bbs3.chinaunix.net/thread-1464828-1-2.html


 bitterness 回复于:2009-06-25 10:59:25

有些进程死去,是KILL不掉的
会成为类型这种<defunct>


 小公猫 回复于:2009-06-25 12:38:50

不错,:mrgreen:




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=77028
转载请注明作者名及原文出处