文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>perl对windows系统进程进行监控

perl对windows系统进程进行监控

时间:2009-08-01  来源:huifeideluotuo

因为项目需要,要对windows下面的多个进程进行监控,当进程死掉的时候,自动启动该进程,记录下log,并通知系统管理员。

其实该脚本主要是使用了cpan的 Win32::Process::List 模块.

脚本如下:

#!C:/perl/bin/perl.exe

use strict;
use warnings;
use Net::SMTP_auth;
use POSIX "strftime";
use Win32::Process::List;

my $mailhost = 'smtp.163.com';
my $mailfrom = 'xxxx@163.com';
my @mailto   = 'xxxx@163.com';
my $subject  = '';

my $user   = 'xxxx';
my $passwd = 'xxxx';

my $err_log = 'C:/monitor_process/process_err_log';

use constant PROCESSLIST => ('notepad.exe',
                             'firefox.exe');

foreach my $process (PROCESSLIST) {
    if ( !&check_process_alive( $process ) ) {

         print  "$process is fail!" . "\n";

         open FH, ">> $err_log" or die $!;

         print FH "-" x 80 . ">", "\n";
         my $check_time = &current_time();
         print FH  "check at $check_time\n";
         print FH  "$process is fail!" . "\n";
        
         sleep 1;
         system('start ' .$process);

         $subject = "$process is fail";
         &SendMail();

    }
}

##############################
# current time
##############################
sub current_time() {

    my $time_now = POSIX::strftime("[%Y-%m-%d %H:%M:%S]", localtime);
    return $time_now;
}

###############################
# Server process is alive check
###############################
sub check_process_alive {
    my ( $process ) = @_;
    my $P = Win32::Process::List->new();

    if ( !$P->GetProcessPid("$process") ) {
        return 0;
    }
    return 1;
}

##############################
# Send notice mail
##############################
sub SendMail() {

    my $smtp = Net::SMTP_auth->new( $mailhost, Timeout => 120, Debug => 1 )
      or die "Error.\n";
    $smtp->auth( 'LOGIN', $user, $passwd );

    foreach my $mailto (@mailto) {
        $smtp->mail($mailfrom);
        $smtp->to($mailto);
        $smtp->data();
        $smtp->datasend("To: $mailto\n");
        $smtp->datasend("From:$mailfrom\n");
        $smtp->datasend("Subject: $subject\n");
        $smtp->datasend("\n");
        $smtp->dataend();
    }

    $smtp->quit;
}
相关阅读 更多 +
排行榜 更多 +
突击队3蛇小队

突击队3蛇小队

飞行射击 下载
剌激地铁逃生

剌激地铁逃生

飞行射击 下载
口袋枪神安卓版

口袋枪神安卓版

飞行射击 下载