两段用来启动/重启Linux下Tomcat的Perl脚本
两段代码,第二段比较好些。
下面是Split输出结果方式的代码:
#!/usr/local/bin/perl #Date:2015-07-07 print "Begin to restart tomcat\n"; my $output = `ps -ef|grep tomcat`; print "$output\n"; my $pid="-1"; my @arr=split(/\n/,$output); $length=@arr; $index=0; while($index<$length){ $line=$arr[$index]; my @arrLine=split(/\s+/,$line); if($arrLine[0] eq "root" && $arrLine[2] eq "1" && $arrLine[3] eq "99"){ $pid=$arrLine[1]; print "Found tomcat's pid=$pid\n"; } $index++; } if($pid eq "-1"){ print "No tomcat run,will start it\n"; system("/home/rtt8/apache-tomcat-7.0.42/bin/startup.sh"); print "Tomcat was started\n"; }else{ system("kill -9 $pid"); print "Tomcat was killed\n"; system("/home/rtt8/apache-tomcat-7.0.42/bin/startup.sh"); print "Tomcat have been restarted\n"; }
下面是正则表达式查找输出结果的代码
#!/usr/local/bin/perl #Date:2015-07-07 print "1.Find running tomcat\n"; my $output = `ps -ef|grep tomcat`; print "$output\n"; if($output=~/(root)\s+(\d+)\s+(1)\s+(99)/){ $pid=$2; print "Found running tomcat's pid=$pid\n"; system("kill -9 $pid"); print "Running tomcat was killed\n"; }else{ print "No running tomcat,will start it\n"; } print "\n2.Start tomcat\n"; system("/home/rtt8/apache-tomcat-7.0.42/bin/startup.sh"); print "Tomcat was started.\n";