Perl 监控 tomcat,可以安心回家过年了

   公司的struts 2 项目,偶尔会出现out of memory异常,导致系统崩溃,所以写了个脚本监控一下。间隔10秒Post一次数据,连续5次不是返回 200 OK,就重启tomcat。

use AnyEvent;
use AnyEvent::HTTP;
use POSIX qw(strftime);
use strict; 
use warnings; 
 
my $count = 0;                     #计数
my $startbat   = 'startup.bat';    #启动tomcat的bat
my $stopbat   = 'shutdown.bat';    #关闭tomcat的bat
my $link  = "http://localhost:8080/GpsOverSpeedSearch/"; #POST的URL
 
sub checkIsAlive { 
  http_request POST => $link,
               timeout => 30, sub {
    my ($body, $hdr) = @_;
    
	if ($hdr->{Status} =~ /^2/) {
      $count = 0;
	  print(strftime("%Y-%m-%d %H:%M:%S ", localtime));
	  print "tomcat is alive!\n";
    } else {
	  print(strftime("%Y-%m-%d %H:%M:%S ", localtime));
	  print "lost link for the server!\n";
	  if($count++ > 3) {
	    print(strftime("%Y-%m-%d %H:%M:%S ", localtime));
	    print "it will be restart tomcat!\n";
		system($stopbat);
		system($startbat);
		$count = 0;
	  } 
	}
   };
}

my $cv    = AnyEvent->condvar;
 
my $time_watcher = AnyEvent->timer (
  after => 10, 
  interval => 10, 
  cb => \&checkIsAlive
);

$cv->recv;

把代码丢服务器上,可以安心回家过年咯。很少写Perl了,要是脚本异常崩溃别喷我呀。

转载于:https://my.oschina.net/huanghongqiao/blog/612887

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值