I need a perl script to stop and start program (310 Views)
Reply
Super Advisor
'chris'
Posts: 1,698
Registered: ‎05-10-2003
Message 1 of 15 (310 Views)
Accepted Solution

I need a perl script to stop and start program

hi

I need a perl script to stop and start program:

-------------------------------------------------------
# stop PROGRAM

sh /home/ntop.sh stop


# start PROGRAM

sh /home/ntop.sh start
-------------------------------------------------------

I'd like first stop the program and start it again using apache & browser.

kind regards
chris

Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 2 of 15 (310 Views)

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

perl -e '$cc = system("/home/ntop.sh start"); exit($cc);'

I trust you can figure out the stop program.
If it ain't broke, I can fix that.
Please use plain text.
Super Advisor
'chris'
Posts: 1,698
Registered: ‎05-10-2003
Message 3 of 15 (310 Views)

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

thanks,

I've written following perl script:

----------------------------------------------------------
#!/usr/bin/perl -w

use CGI::Carp qw(fatalsToBrowser);
use CGI;
my $query = new CGI;

$bb = system("/home/ntop.sh stop &");

exit($bb);

$cc = system("/home/ntop.sh start &");

exit($cc);
----------------------------------------------------------


if execute it from command line:

# perl ntop.cgi

then ntop starts,

but via browser, script be executed without errors, but ntop won't start.


# chown www ntop.cgi

doesn't help !

kind regards
chris

Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 4 of 15 (310 Views)

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

#!/usr/bin/perl -w

use CGI::Carp qw(fatalsToBrowser);
use CGI;
my $query = new CGI;

$bb = system("/home/ntop.sh stop &");

exit($bb); #### This exit terminate the program!!! Nothing past this point is done

$cc = system("/home/ntop.sh start &");

exit($cc);
-----------------------------------------

You made thimgs complicated using the & to stop in background.

Change to this:

#!/usr/bin/perl -w

use CGI::Carp qw(fatalsToBrowser);
use CGI;
my $query = new CGI;

my $bb = system("/home/ntop.sh stop");
sleep(10);
$cc = system("/home/ntop.sh start &");
exit($cc);

or this:
#!/usr/bin/perl -w

use CGI::Carp qw(fatalsToBrowser);
use CGI;
my $query = new CGI;

my $bb = system("/home/ntop.sh stop &");
wait; # wait for background task to finish
$cc = system("/home/ntop.sh start &");
exit($cc);

If it ain't broke, I can fix that.
Please use plain text.
Super Advisor
'chris'
Posts: 1,698
Registered: ‎05-10-2003
Message 5 of 15 (310 Views)

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

thanks,
---------------------------------------------------------------------------------------
#!/usr/bin/perl -w

use CGI::Carp qw(fatalsToBrowser);
use CGI;
my $query = new CGI;

print $query->header;
print "\n";
print "\n";
print "Repair\n";
print "\n";
print "\n";
print "\n";
print "
";
print "


";
print "


";

my $bb = system("/home/ntop.sh stop");
sleep(5);
$cc = system("/home/ntop.sh start &");

print "";
print "";

exit($cc);
---------------------------------------------------------------------------------------

it works excellent from command line,
but it it doesn't start ntop via browser.

what's wrong ?
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 6 of 15 (310 Views)

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

Add a
print "Status: ",$cc,"\n";

After the system call. You should at least then see the exit status. I suspect you are missing environment variables or ntop expects a tty device as stdin. Put probes that write to a file in your ntop.sh script.

If it ain't broke, I can fix that.
Please use plain text.
Super Advisor
'chris'
Posts: 1,698
Registered: ‎05-10-2003
Message 7 of 15 (310 Views)

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

I get the output:

Status: 0 ntop

I think the www user has no rights
to execute "system" via browser,
because I have in the Apache error log:

[Thu Jul 21 23:43:54 2005] [error] [client 192.168.0.105] Permission denied

do you know howto change that ?

Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 8 of 15 (310 Views)

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

Do something like "ls -l /etc" inside your system command as a test.
If it ain't broke, I can fix that.
Please use plain text.
Super Advisor
'chris'
Posts: 1,698
Registered: ‎05-10-2003
Message 9 of 15 (310 Views)

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

# ls -l /etc
total 602
-rw-r--r-- 1 root wheel 32 Jul 19 12:55 .directory
drwxr-xr-x 3 root wheel 512 Jul 18 22:54 .java
drwxr-xr-x 2 root wheel 512 Jul 20 23:02 X11
lrwxrwxrwx 1 root wheel 12 Jul 15 23:38 aliases -> mail/aliases
-rw-r--r-- 1 root wheel 65536 Jul 18 19:04 aliases.db
-rw-r--r-- 1 root wheel 209 May 8 09:05 amd.map
-rw-r--r-- 1 root wheel 1234 May 8 09:05 apmd.conf
-rw-r--r-- 1 root wheel 231 May 8 09:05 auth.conf
drwxr-xr-x 2 root wheel 512 Jul 15 23:38 bluetooth
-rw-r--r-- 1 root wheel 737 May 8 09:05 crontab
-rw-r--r-- 1 root wheel 108 May 8 09:05 csh.cshrc
-rw-r--r-- 1 root wheel 481 May 8 09:05 csh.login
-rw-r--r-- 1 root wheel 110 May 8 09:05 csh.logout
drwxr-xr-x 2 root wheel 512 Jul 15 23:38 defaults
-rw-r--r-- 1 root wheel 4846 May 8 09:05 devd.conf
-rw-r--r-- 1 root wheel 2071 May 8 09:05 devfs.conf
-rw-r--r-- 1 root wheel 267 May 8 09:05 dhclient.conf
-rw-r--r-- 1 root wheel 6621 May 8 09:05 disktab
-rw-rw-r-- 1 root operator 0 May 8 09:05 dumpdates
-rw-r--r-- 1 root wheel 142 May 8 09:05 fbtab
-rw-r--r-- 1 root wheel 391 Jul 15 23:50 fstab
-rw-r--r-- 1 root wheel 245 May 8 09:05 ftpusers
-rw-r--r-- 1 root wheel 5904 May 8 09:05 gettytab
drwxr-xr-x 2 root wheel 512 Jul 15 23:38 gnats
-rw-r--r-- 1 root wheel 489 Jul 16 15:09 group
-rw-r--r-- 1 root wheel 60 Jul 15 22:29 host.conf
-rw-r--r-- 1 root wheel 146 Jul 20 21:25 hosts
-rw-r--r-- 1 root wheel 3237 Jul 21 01:39 hosts.allow
-rw-r--r-- 1 root wheel 111 May 8 09:05 hosts.equiv
-rw-r--r-- 1 root wheel 99 May 8 09:05 hosts.lpd
-rw-r--r-- 1 root wheel 5319 May 8 09:05 inetd.conf
drwx------ 2 root wheel 512 Jul 15 23:38 isdn
-rw-r--r-- 1 root wheel 837 May 8 09:03 localtime
-rw-r--r-- 1 root wheel 619 May 8 09:05 locate.rc
-rw-r--r-- 1 root wheel 1847 May 8 09:05 login.access
-rw-r--r-- 1 root wheel 6522 May 8 09:05 login.conf
-rw-r--r-- 1 root wheel 65536 May 8 09:05 login.conf.db
-rw-r--r-- 1 root wheel 564 May 8 09:05 mac.conf
drwxr-xr-x 2 root wheel 512 Jul 17 12:51 mail
-rw-r--r-- 1 root wheel 106 May 8 09:05 mail.rc
-rw-r--r-- 1 root wheel 91 Jul 18 19:09 make.conf
-rw-r--r-- 1 root wheel 0 Jul 16 15:20 make.conf.bak
-rw-r--r-- 1 root wheel 1087 Jul 16 15:27 manpath.config
-rw-r--r-- 1 root wheel 946 Jul 16 15:20 manpath.config.bak
-rw------- 1 root wheel 1845 Jul 21 00:20 master.passwd
-rw-r--r-- 1 root wheel 1114 Jul 15 22:29 motd
drwxr-xr-x 2 root wheel 512 Jul 15 23:38 mtree
lrwxrwxrwx 1 root wheel 23 Jul 15 23:38 namedb -> ../var/named/etc/namedb
-rw-r--r-- 1 root wheel 783 May 8 09:05 netconfig
-rwxr-xr-x 1 root wheel 2362 May 8 09:05 netstart
-rw-r--r-- 1 root wheel 13696 May 8 09:05 network.subr
-rw-r--r-- 1 root wheel 365 May 8 09:05 networks
-rw-r--r-- 1 root wheel 1937 May 8 09:05 newsyslog.conf
-rw------- 1 root wheel 1701 May 8 09:05 nsmb.conf
-rw-r--r-- 1 root wheel 113 Jul 15 22:29 nsswitch.conf
drwx------ 2 root wheel 512 May 8 09:00 ntp
-rw------- 1 root wheel 432 May 8 09:05 opieaccess
-rw-r--r-- 1 root wheel 0 Jul 15 22:36 opiekeys
drwxr-xr-x 2 root wheel 512 Jul 15 23:38 pam.d
-rw-r--r-- 1 root wheel 1607 Jul 21 00:20 passwd
-rwxr-xr-x 1 root wheel 5814 May 8 09:05 pccard_ether
drwxr-xr-x 6 root wheel 512 Jul 15 23:38 periodic
-rw-r--r-- 1 root wheel 177 Jul 18 19:22 periodic.conf
-rw-r--r-- 1 root wheel 3567 May 8 09:05 pf.conf
-rw-r--r-- 1 root wheel 22556 May 8 09:05 pf.os
-rw-r--r-- 1 root wheel 293 May 8 09:05 phones
drwxr-xr-x 2 root wheel 512 Jul 15 23:38 ppp
-rw-r--r-- 1 root wheel 2058 May 8 09:05 printcap
-rw-r--r-- 1 root wheel 619 May 8 09:05 profile
-rw-r--r-- 1 root wheel 5807 May 8 09:05 protocols
-rw-r--r-- 1 root wheel 40960 Jul 21 00:20 pwd.db
-rw-r--r-- 1 root wheel 2716 May 8 09:05 rc
-rw-r--r-- 1 root wheel 5298 May 8 09:05 rc.bsdextended
-rw-r--r-- 1 root wheel 942 Jul 20 21:33 rc.conf
-rw-r--r-- 1 root wheel 1114 Jul 20 21:25 rc.conf~
drwxr-xr-x 2 root wheel 2560 Jul 15 23:38 rc.d
-rw-r--r-- 1 root wheel 9569 May 8 09:05 rc.firewall
-rw-r--r-- 1 root wheel 8905 May 8 09:05 rc.firewall6
-rwxr-xr-x 1 root wheel 2165 May 8 09:05 rc.resume
-rw-r--r-- 1 root wheel 5785 May 8 09:05 rc.sendmail
-rw-r--r-- 1 root wheel 3211 May 8 09:05 rc.shutdown
-rw-r--r-- 1 root wheel 30905 May 8 09:05 rc.subr
-rwxr-xr-x 1 root wheel 2209 May 8 09:05 rc.suspend
-rw-r--r-- 1 root wheel 2392 May 8 09:05 remote
-rw-r--r-- 1 root wheel 42 Jul 20 21:25 resolv.conf
lrwxrwxrwx 1 root wheel 13 Jul 15 23:38 rmt -> /usr/sbin/rmt
-rw-r--r-- 1 root wheel 1674 May 8 09:05 rpc
-rw-r--r-- 1 root wheel 73410 May 8 09:05 services
-rw-r--r-- 1 root wheel 237 Jul 16 00:04 shells
drwxr-xr-x 2 root wheel 512 May 8 09:00 skel
-rw------- 1 root wheel 40960 Jul 21 00:20 spwd.db
drwxr-xr-x 2 root wheel 512 Jul 15 22:34 ssh
drwxr-xr-x 2 root wheel 512 Jul 15 23:38 ssl
-rw-r--r-- 1 root wheel 367 May 8 09:05 sysctl.conf
-rw-r--r-- 1 root wheel 1339 May 8 09:05 syslog.conf
lrwxrwxrwx 1 root wheel 23 Jul 15 23:38 termcap -> /usr/share/misc/termcap
-rw-r--r-- 1 root wheel 13 Jul 20 22:48 timezone
-rw-r--r-- 1 root wheel 7658 May 8 09:05 ttys
-rw-r--r-- 1 root wheel 2032 May 8 09:05 usbd.conf
-r--r--r-- 1 root wheel 0 Jul 15 23:42 wall_cmos_clock

Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 10 of 15 (310 Views)

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

Okay, that's rather convincing data that this is not a simple permission problem. Start probing your ntop.sh shell and redirect both stderr and stdout to a logfile.
If it ain't broke, I can fix that.
Please use plain text.
Super Advisor
'chris'
Posts: 1,698
Registered: ‎05-10-2003
Message 11 of 15 (310 Views)

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 ?
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 12 of 15 (310 Views)

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.
Please use plain text.
Super Advisor
'chris'
Posts: 1,698
Registered: ‎05-10-2003
Message 13 of 15 (310 Views)

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
-----------------------------------------------------------------------------------------------------
Please use plain text.
Super Advisor
'chris'
Posts: 1,698
Registered: ‎05-10-2003
Message 14 of 15 (310 Views)

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.


Please use plain text.
Super Advisor
'chris'
Posts: 1,698
Registered: ‎05-10-2003
Message 15 of 15 (310 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation