免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: gaochong
打印 上一主题 下一主题

根据日志文件监控进程(已初步解决,附named-nanny.pl供各位参考) [复制链接]

论坛徽章:
0
11 [报告]
发表于 2009-05-30 09:41 |只看该作者

回复 #10 gaochong 的帖子

怎么没人呢?
各位大师帮忙看看,谢谢啦

论坛徽章:
0
12 [报告]
发表于 2009-05-30 10:16 |只看该作者
原帖由 gaochong 于 2009-5-30 09:41 发表
怎么没人呢?
各位大师帮忙看看,谢谢啦


细心区别 PID PPID :wink: 别乱Kill 迟早会出事的!
BTW:你很喜欢用goto吗

论坛徽章:
0
13 [报告]
发表于 2009-05-30 11:19 |只看该作者
原帖由 xuanfei 于 2009-5-30 10:16 发表


细心区别 PID PPID :wink: 别乱Kill 迟早会出事的!
BTW:你很喜欢用goto吗:lol::" />:" />



我把goto改成if语句了,如下:

#!/usr/bin/perl -w


use strict;

$ENV{'LD_LIBRARY_PATH'}="../commonlib:/ldap/ds5/lib:/usr/local/mysql/lib/mysql:/usr/local/ssl/lib:/usr/lib:/usr/ucblib";    #设置环境变量

my $file="/logs/log.dat";

for (;;) {
        my $mtime1=localtime ((stat($file))[9]);
        print "$mtime1\n";
        sleep 5;
        my $mtime2=localtime ((stat($file))[9]);
        print "$mtime2\n";
        if ($mtime1 eq $mtime2) {        #如果被修改时间相等,则执行。这是为了测试,见后边执行结果
                 chomp (my $proc=`ps -ef | grep -i "ua_aaaService -r -l -b -f ../conf/config.xml" | grep -v grep | wc -l`);
                if ($proc==1) {
                          foreach (`ps -ef | grep -i "ua_aaaService -r -l -b -f ../conf/config.xml" | grep -v grep`) {
                        print $_;
                        my($pid)=/\s+\S+\s+(\S+)\s+.*/;
                        print "$pid\n";
                        #system ("kill -9 $pid");               #测试用,注释掉
                        chomp (my $date=`date`);
                        open FILE,">>/export/home/g/log"
                                or die "$!";
                        printf FILE "$date restart-radius(pid alive)\n";
                        close FILE;
                        chdir "/export/home/boss/ua/bin/" or die "$1";
                        #system ("./ua_aaaService -r -l -b -f ../conf/config.xml");
                        }
                } elsif ($proc==0) {
                        print "no pid\n";
                        chomp (my $date2=`date`);
                        open FILE,">>/export/home/g/log"
                                or die "$!";
                        printf FILE "$date2 restart-radius(no pid)\n";
                        close FILE;
                        chdir "/export/home/boss/ua/bin/" or die "$1";
                        #system ("./ua_aaaService -r -l -b -f ../conf/config.xml");
                } else {};
             }

        }
next;
}



程序中的pid是对的。目前的情况是进程存在但不能服务请求,所以要kill否则怎么办呢?

测试用的执行结果如下:
-bash-3.00$ ./check.pl
Thu May 28 18:38:28 2009
Thu May 28 18:38:28 2009
    g 23231     1   0   5月 28 ?           0:04 ./ua_aaaService -r -l -b -f ../conf/config.xml
23231
Thu May 28 18:38:28 2009
Thu May 28 18:38:28 2009
    g 23231     1   0   5月 28 ?           0:04 ./ua_aaaService -r -l -b -f ../conf/config.xml
23231
Thu May 28 18:38:28 2009
Thu May 28 18:38:28 2009
    g 23231     1   0   5月 28 ?           0:04 ./ua_aaaService -r -l -b -f ../conf/config.xml
23231
Thu May 28 18:38:28 2009


[ 本帖最后由 gaochong 于 2009-5-30 12:30 编辑 ]

论坛徽章:
0
14 [报告]
发表于 2009-05-30 13:21 |只看该作者

回复 #10 gaochong 的帖子

看看 log 没次都死在同一个位置么?
或者每次死的时间一样么

论坛徽章:
0
15 [报告]
发表于 2009-05-30 13:22 |只看该作者

回复 #10 gaochong 的帖子

看看 log 没次都死在同一个位置么?
或者每次死的时间一样么

论坛徽章:
0
16 [报告]
发表于 2009-05-30 13:46 |只看该作者
原帖由 DQP 于 2009-5-30 13:22 发表
看看 log 没次都死在同一个位置么?
或者每次死的时间一样么



我说的死掉是Perl监控进程死掉,而且log记录的是我要监控的进程重启的记录。

所以楼上说的好像。。。。(没什么用)。要不你再仔细看看代码?

论坛徽章:
0
17 [报告]
发表于 2009-05-30 14:34 |只看该作者

回复 #16 gaochong 的帖子

嗯 说的就是这个监控进程
看看死在哪了

论坛徽章:
0
18 [报告]
发表于 2009-05-30 14:42 |只看该作者
原帖由 DQP 于 2009-5-30 14:34 发表
嗯 说的就是这个监控进程
看看死在哪了



请教,怎么看perl监控进程死在哪里呢???急!谢谢!

论坛徽章:
0
19 [报告]
发表于 2009-05-30 14:55 |只看该作者
刚才服务器进程又丢失,但perl监控进程也没有起到作用,也不知道什么时候perl进程丢失了。



从程序代码上,哪些地方还可以再改进呢?
谢谢了!

论坛徽章:
0
20 [报告]
发表于 2009-05-30 15:10 |只看该作者
如果是长期要运行的话,建议写成daemon进程。那样可能会稳定些. 下面是个例子

#!/usr/bin/perl
use strict;
use IO::File;
use IO::Socket;
use Chatbot::Eliza;
use POSIX qw(WNOHANG setsid);
use constant PORT => 9000;
use constant PID_FILE => '/var/run/test.pid';
my $quit = 0;

$SIG{CHLD} = sub { while (waitpid(-1,WNOHANG)>0){}};
$SIG{TERM} = $SIG{INT} = sub { $quit++;};

my $fh = open_pid_file(PID_FILE);
my $listen_socket= IO::Socket::INET->new(LocalPort => shift || PORT,
                      Listen => 20,
                      Proto => 'tcp',
                      Reuse => 1,
                      Timeout => 60*60
               );
die "Can not Create a listenling socket: $@" unless $listen_socket;

warn "$0 starting...\n";
my $pid = become_daemon();
print $fh $pid;
close($fh);
for(;{
if(my $connection = $listen_socket->accept){
   if((my $child = fork()) == 0){
       $listen_socket->close;
       interact($connection);
       exit(0);
   }
   $connection->close;
}
}

sub become_daemon(){
   die "can't fork" unless defined ( my $child = fork );
   exit 0 if $child;
   setsid();
   open(STDIN,"</dev/null";
   open(STDOUT,">/dev/null";
   open(STDERR,">&/dev/null";
   chdir '/';
   umask(0);
   $ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin';
   return $$;
}

sub open_pid_file(){
   my $file = shift;
   if( -e $file) {
   my $fh = IO::File->new($file)||return;
   my $pid=<$fh>;
   die "Server already running with PID $pid" if kill 0 => $pid;
   warn "Removing PID file for defunct server process $pid\n";
   die "Can not unlink PID file $file" unless -w $file && unlink $file;
   }
   return IO::File->new($file,O_WRONLY|O_CREAT|O_EXCL,0644)
   or die "Can not create $file: $!\n"
}  

sub interact(){
   my $sock = shift;
   my $string;
   #STDIN->fdopen($sock,"<" or die "Can not reopen STDIN: $!";
   #STDOUT->fdopen($sock,">" or die "Can not reopen STDOUT: $!";
   #STDERR->fdopen($sock,">" or die "Can not reopen STDERR: $!";
   #$| = 1;
   recv $sock,$string,1024,0;
   send $sock,$string,0;
}
  
END {unlink PID_FILE if $$ == $pid;}

[ 本帖最后由 Perl_Er 于 2009-5-30 15:13 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP