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 = <PID>;


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);
}

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

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/;
}

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.