Click to See Complete Forum and Search --> : howto check for a process and stop it using perl ?


ccc
07-26-2005, 09:49 AM
hi

howto check for a process and only if it's running to stop it
using a perl script ?

kind regards
ccc

ccc
07-26-2005, 08:20 PM
I've this perl script:
#!/usr/bin/perl -w

use strict;
use warnings;
use CGI;
my $query = new CGI;
use CGI::Carp qw(fatalsToBrowser);
my $process = "ntop";

$|=1;
print $query->header;
print "<html>\n";
print "<head>\n";
print "<title>stop ntop</title>\n";
print "</head>\n";
print "<body bgcolor='#c0c0d0'>\n";
print "<center>";

my $return = `ps -eaf |grep $process |grep -v grep`;

my $stop = `/usr/local/bin/sudo /usr/local/www/cgi-bin/ntop/ntop.sh stop`;

if ($return ne "" ) {
system ($stop) or die "...";
}

if ($? == -1) {
print "failed to execute: $!\n";
} elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
} else {
printf "child exited with value %d\n", $? >> 8;
}

print "<font face=\"arial,helvetica\" size=2 color=\"#006600\">status:</font><font face=\"arial,helvetica\" size=2 color=\"#FF0000\"> ntop was killed !</font>\n";
print "</center>";
print "</body>";
print "</html>";


howto change it to get the output status in the browser:

"ntop is not running"

or

"ntop was killed"

ccc
07-27-2005, 08:30 AM
I've tried something:
my $return = `ps -eaf |grep $process |grep -v grep`;

my $stop = `/usr/local/bin/sudo /usr/local/www/cgi-bin/ntop/ntop.sh stop`;

if ($return ne "" ) {
system ($stop) or die "...";
}

if ($stop == -1) {
print "failed to execute: $!\n";
} elsif ($stop & 127) {
printf "ntop was killed with signal %d, %s coredump\n",
($stop & 127), ($stop & 128) ? 'with' : 'without';
} else {
printf "ntop exited with value %d\n", $stop >> 8;
}
but it won't really work and I get only this output:

"ntop exited with value 0"

rigadon
07-27-2005, 04:43 PM
Hi

I don't really know much about this but I guess you might want to try something like this:

my $return = `ps -eaf |grep $process |grep -v grep`;
if ($return)
{ my $stop = system('/usr/local/bin/sudo /usr/local/www/cgi-bin/ntop/ntop.sh stop');
if ($stop == -1)
{ print "failed to execute: $!\n";
} elsif ($stop & 127)
{ printf "ntop was killed with signal %d, %s coredump\n", ($stop & 127), ($stop & 128) ? 'with' : 'without';
} else
{ printf "ntop exited with value %d\n", $stop >> 8;
}
}
Or I could be completely wrong :p

ccc
01-14-2006, 06:24 AM
thanks, this code seems to work:
my $return = 'ps -eaf |grep $process |grep -v grep';

my $stop = '/usr/local/bin/sudo /usr/local/www/cgi-bin/ntop/ntop.sh stop';

#if ($return ne "" ) {
# system ($stop) and die "...";
#}

if ($return ne "" ) {
system($stop) == 0 or die "Stop failed: $!"
}

sleep(3);

if ($stop == -1) {
print "failed to execute: $!\n";
} elsif ($stop & 127) {
printf "died with signal %d, %s coredump\n",
($stop & 127), ($stop & 128) ? 'with' : 'without';
} else {
printf " exited with value %d\n", $stop >> 8;
}
greetings
ccc