954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PROCESS CHECKING PROBLEM using PERL

Hi All
I am trying to write a perl script which will check some particular process is running in a machine or not. If the processes are running properly then its ok if the processes are not running then the script will start those processes and send a mail to a particular e-mail id mentioning the current status.
The problem I am facing that I have written the code for a single process but I am not able to run the code for multiple process.Another thing that I am checking the process by using PID but if I want to check the process by using process names then what to do?

I am copying the code what i have written.

#!/usr/bin/perl
use Proc::ProcessTable;
$t = new Proc::ProcessTable;

$command = '/etc/init.d/portmap start';
$pidfile = "/home/anadhikary/123.txt";
$notice = "1"; # 0=no 1=yes
$mail = 'anirban.adhikary@gmail.com';
$mail_prog = "/usr/sbin/sendmail";

open (PID, "$pidfile");


$assppid = ;

close PID;
foreach $proc ( @{$t->table} )
{
$tmp = $proc->pid;
if ($assppid == $tmp)
{
$notice=0;
exit;
}
}

system ("$command");
if ($notice == 1)
{
open (MAIL, "|$mail_prog -t");
print MAIL "To: $mail\n";
print MAIL "Subject: Software need restart\n";
print MAIL "\nSoftware was restartet\n" ;
close (MAIL);
}

ratul
Newbie Poster
3 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

first check if the module you are using has a method to find the process by name.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 
but if I want to check the process by using process names then what to do?

if i correctly understand your goal, this code is what you need:

# get processes names
my @ps = `ps aux`;

# for example, is perl running?
while ( my $ps = shift @ps )
{
print "Perl is running now" if $ps =~ /\/usr\/bin\/perl/;
}

cerf_machine
Newbie Poster
8 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

There are some modules on CPAN that might help. I wrote a reasonably sophisticated process monitor with the Proc::ProcessTable module.

It may be a bit complicated for your needs just now though.

BobUillean
Newbie Poster
5 posts since Dec 2007
Reputation Points: 10
Solved Threads: 1
 

The original question is nearly 2 months old now, I doubt the OP is still interested in replies, but maybe.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

Yeah - didn't notice that before I replied. It is even worse than that though - in the original code Proc::ProcessTable is used. Doh.

BobUillean
Newbie Poster
5 posts since Dec 2007
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You