you're reading...
Linux Tips and Tricks, Scripting

Simple Perl Script To Kill a Process Which Has Gone Wild

Purpose: Kills specified process when it has gone wild and consumed a lot of server resources.

In this example, I have known process which goes wild once in a while so I have to automate the killing process.


use Net::SMTP;
use strict;
use warnings;

our $one_min_avg;
our $five_min_avg;

my $oneminlimit = 15;
my $fiveminlimit = 10;


if ($one_min_avg > $oneminlimit && $five_min_avg > $fiveminlimit) {

sub get_load_average {
open(LOAD, "/proc/loadavg") || die "Error: $!","\n";
my $load_avg = ;
close LOAD;
my @cpuload = split /\s/, $load_avg;
$one_min_avg = $cpuload[0];
$five_min_avg = $cpuload[1];

sub kill_processes {
open(PS, "ps aux|grep [a]genttimer|") || die "Error: $!";
my @pids;

while () {
push @pids, [ split ];

foreach (@pids) {
my $pid = "@$_[1]";
#print "$pid\n";
`kill -9 $pid`;


sub send_mail {
my $smtp = Net::SMTP->new('localhost');
$smtp->datasend("To: mailer\@mydomain.com\n");
$smtp->datasend("Subject: ALERT: SERVER Load is CRITICAL\n");
$smtp->datasend("SERVER Load is in CRITICAL state. Below are the current Load Values.\n\n");
$smtp->datasend("1Min Load Avg is: $one_min_avg\n");
$smtp->datasend("5Min Load Avg is: $five_min_avg\n\n");
$smtp->datasend("To prevent server from being inaccessible, rouge processess were killed.\n");
$smtp->datasend("Please check status after 5 minutes.\n\nThanks!\n");



No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s