Operating System - HP-UX
1752777 Members
6402 Online
108789 Solutions
New Discussion юеВ

Re: I need a perl script to stop and start program

 
SOLVED
Go to solution
'chris'
Super Advisor

Re: I need a perl script to stop and start program

but how can I do this ?

I mean to do this as a root user ?
A. Clay Stephenson
Acclaimed Contributor

Re: I need a perl script to stop and start program

I didn't write your ntop.sh and I didn't write ntop so how can I possibly know what you need to do? I do know how to find out and that is to add
exec 1>>/var/tmp/nlog.txt
exec 2>&1 to the top of your ntop.sh

You then put plenty of echo in the script and everything inclding stderr will now go to /var/tmp/nlog.txt and you look there for the problems. This is debugging 101.
If it ain't broke, I can fix that.
'chris'
Super Advisor

Re: I need a perl script to stop and start program

this is ntop.sh script:
-----------------------------------------------------------------------------------------------------
#!/bin/sh
interfaces=''

# User to run ntop as; leave blank for root
userid='www'

# [IP:]port for serving HTTP; set to '0' to disable
#http_port='3000'

# [IP:]port for serving HTTPS; set to '0' to disable
# The certificate is /usr/local/etc/ntop/ntop-cert.pem
https_port='10.10.3.77:3001'

# Directory for ntop.access.log
logdir='/var/log'

# Specify any additional arguments here - see ntop(8)
additional_args=''

#
# End of user-configurable variables
#

args='-d -L --set-pcap-nonblocking --skip-version-check'

[ ! -z $interfaces ] && args="$args -i $interfaces"
[ ! -z $http_port ] && args="$args -w $http_port"
[ ! -z $https_port ] && args="$args -W $https_port"
[ ! -z $logdir ] && args="$args -a ${logdir}/ntop.access.log"
[ ! -z $userid ] && args="$args -u $userid"
[ ! -z "$additional_args" ] && args="$args $additional_args"

case "$1" in
start)
# is it the first time we run ntop
[ ! -e /var/db/ntop/ntop_pw.db ] && {
# just in case...
[ ! -d /var/db/ntop ] && {
echo "Reinstalling database directory"
mkdir -p /var/db/ntop
chown -R $userid:$userid /var/db/ntop
}
/usr/local/bin/ntop -u $userid -A || exit 1
echo "Now we can start ntop!"
}
if [ -d $logdir ]; then
touch ${logdir}/ntop.access.log
chown $userid ${logdir}/ntop.access.log
fi
if [ -x /usr/local/bin/ntop ]; then
/usr/local/bin/ntop $args > /dev/null 2>&1 &
echo -n ' ntop'
fi
;;
stop)
killall ntop > /dev/null 2>&1 && echo -n ' ntop'
;;
*)
echo "Usage: `basename $0` {start|stop}" >&2
exit 64
;;
esac

exec 1>>/var/tmp/nlog.txt
exec 2>&1 to the top of your ntop.sh

exit 0
-----------------------------------------------------------------------------------------------------
'chris'
Super Advisor

Re: I need a perl script to stop and start program

if I put to the top of this srcipt, then I cannot start ntop from command line.


'chris'
Super Advisor

Re: I need a perl script to stop and start program

I've written these scripts and it seems to work on freeBSD 6.1 using sudo:


#!/usr/bin/perl -w

# ntop startup script

use strict;
use warnings;
use CGI;
#use CGI::Carp qw(fatalsToBrowser);

my $query = new CGI;
my $process = "ntop";

# write the log
BEGIN
{
use CGI::Carp qw(carpout);
my $errorlog = "/var/tmp/ntop_startup_log.txt";
open(LOG, ">$errorlog") or die("Unable to open $errorlog: $!\n");
print LOG "Errors:\n";
carpout(*LOG);
}

$|=1;
print $query->header;
print "\n";
print "\n";
print "ntop startup script\n";
print "\n";
print "<SCRIPT LANGUAGE = \"JavaScript\">\n";
print "\n";
print "</SCRIPT>\n";
print "\n";
print "
";
print "
\n";
print "
pls wait, ntop will be started !
\n";

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

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

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

sleep(10);

print "status:\n";

my $start = system('/usr/local/bin/sudo /usr/local/www/apache22/cgi-bin/ntop/ntop start') and die "...";

print "

";
print "
pls do not forget to shutdown ( kill ), if you're not using
\n";
print "\n";
print "


";
print "pls wait for 10 seconds before you click on this link:\n";
print "

";
print "GO TO NTOP\n";
print "

";
print "KILL NTOP\n";
print "
";
print "";
print "";



and here is the killing script:


#!/usr/bin/perl -w

# ntop kill script

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $query = new CGI;
my $process = "ntop";

# write the log
BEGIN
{
use CGI::Carp qw(carpout);
my $errorlog = "/var/tmp/ntop_kill_log.txt";
open(LOG, ">$errorlog") or die("Unable to open $errorlog: $!\n");
print LOG "Errors:\n";
carpout(*LOG);
}

$|=1;
print $query->header;
print "\n";
print "\n";
print "kill ntop\n";
print "\n";
print "<SCRIPT LANGUAGE = \"JavaScript\">\n";
print "\n";
print "</SCRIPT>\n";
print "\n";
print "
";
print "
\n";
print "
pls wait, ntop will be killed !
\n";
print "


";

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

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

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

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

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

print "\n";
print "

";
print "START NTOP\n";
print "

";
print "
";
print "";
print "";


maka a great day !
chris