LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2005, 07:28 PM   #1
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Rep: Reputation: 51
need a perl script to stop & start program


hi

I need a perl script to stop & 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
cccc
 
Old 07-21-2005, 12:18 PM   #2
hlyrad
Member
 
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44

Rep: Reputation: 15
I assume that what you want to do is allow the user(In reallity the user apache is running under) to stop and start your service?
If this is the case you must give the apache user rights to execute the script.
I use this script via apache/web browser to restart mysql on the same system.

#!/usr/bin/perl
use CGI;
my $query = new CGI;
print $query->header;
print "<html>\n";
print "<head>\n";
print "<title>Repair</title>\n";
print "<STYLE TYPE='text/css'>\n";
print "h3 { color: red }\n";
print "a { text-decoration: none; font: bold 14pt/16pt Ariel, serif }";
print "a:hover { color: red } /* when mouse is over link */";
print "</style>\n";
print "</head>\n";
print "<body bgcolor='#c0c0d0'>\n";
print "<center>";
print "<p><br></p>";
print "<p><br></p>";
`mysqladmin -u flush -preload flush-tables`;
`mysqladmin -u flush -preload shutdown`;
system "mysqld_safe --log &";
print "</center>";
print "</body>";
print "</html>";
 
Old 07-21-2005, 02:50 PM   #3
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
thanks,

I've written following perl script:
Code:
#!/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 !

greetings
cccc
 
Old 07-21-2005, 02:55 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Are you sure ntop start from your script in command-line ?
Wasn't it already started ?

Normally it should exit after the exit($bb);
 
Old 07-21-2005, 03:07 PM   #5
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
sorry, the correct script is:
Code:
#!/usr/bin/perl -w

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

print $query->header;
print "<html>\n";
print "<head>\n";
print "<title>ntop startup script</title>\n";
print "<STYLE TYPE='text/css'>\n";
print "h3 { color: red }\n";
print "a { text-decoration: none; font: bold 14pt/16pt Ariel, serif }";
print "a:hover { color: red } /* when mouse is over link */";
print "</style>\n";
print "</head>\n";
print "<body bgcolor='#c0c0d0'>\n";
print "<center>";
print "<p><br></p>";
print "<p><br></p>";

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

print "</body>";
print "</html>";

exit($cc);
from command line it starts ntop , but via browser ntop doesn't start

Last edited by cccc; 07-21-2005 at 04:05 PM.
 
Old 07-21-2005, 03:59 PM   #6
hlyrad
Member
 
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44

Rep: Reputation: 15
Do any errors show up in your httpd/error_log ?
I suspect youll see an error message like "Access denied"

Try editing your script to capture errors and print them to your browser like so

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




Also
# chown www ntop.cgi


will not work if www doesnt have read acces to the home directory.
I would put the script in the cgi-bin directory where www owns everything.
 
Old 07-21-2005, 04:22 PM   #7
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
I changed the directory and the script, but now I get following error from command line:
Code:
# perl ntop3.cgi
Content-Type: text/html; charset=ISO-8859-1

<html>
<head>
<title>ntop startup script</title>
<STYLE TYPE='text/css'>
h3 { color: red }
a { text-decoration: none; font: bold 14pt/16pt Ariel, serif }a:hover { color: red } /* when mouse is over link */</style>
</head>
<body bgcolor='#c0c0d0'>
<center><p><br></p><p><br></p><h1>Software error:</h1>
<pre>No such file or directory at ntop3.cgi line 22.</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message
and the time and date of the error.

</p>
[Thu Jul 21 23:19:15 2005] ntop3.cgi: No such file or directory at ntop3.cgi line 22.
the new script is:
Code:
#!/usr/bin/perl -w

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

print $query->header;
print "<html>\n";
print "<head>\n";
print "<title>ntop startup script</title>\n";
print "<STYLE TYPE='text/css'>\n";
print "h3 { color: red }\n";
print "a { text-decoration: none; font: bold 14pt/16pt Ariel, serif }";
print "a:hover { color: red } /* when mouse is over link */";
print "</style>\n";
print "</head>\n";
print "<body bgcolor='#c0c0d0'>\n";
print "<center>";
print "<p><br></p>";
print "<p><br></p>";

my $bb = system("/usr/local/www/cgi-bin/ntop/ntop.sh stop") or die ($!);
sleep(5);
$cc = system("/usr/local/www/cgi-bin/ntop/ntop.sh start &") or die ($!);

print "</body>";
print "</html>";

exit($cc);

Last edited by cccc; 07-21-2005 at 05:56 PM.
 
Old 07-21-2005, 05:33 PM   #8
hlyrad
Member
 
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44

Rep: Reputation: 15
I have edited your script as follows.

#!/usr/bin/perl -w

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

print $query->header;
print "<html>\n";
print "<head>\n";
print "<title>ntop startup script</title>\n";
print "<STYLE TYPE='text/css'>\n";
print "h3 { color: red }\n";
print "a { text-decoration: none; font: bold 14pt/16pt Ariel, serif }";
print "a:hover { color: red } /* when mouse is over link */";
print "</style>\n";
print "</head>\n";
print "<body bgcolor='#c0c0d0'>\n";
print "<center>";
print "<p><br></p>";
print "<p><br></p>";

my $bb = `/usr/local/www/cgi-bin/ntop/ntop.sh stop` or die ($!);
sleep(5);
my $cc = `/usr/local/www/cgi-bin/ntop/ntop.sh start` or die ($!);
print "</body>";
print "</html>";


You were not checking the return values of $bb or $cc so just using the `` ticks to encapsulate was better.
In fact you could just do
`/usr/local/www/cgi-bin/ntop/ntop.sh stop` or die ($!);
sleep(5);
`/usr/local/www/cgi-bin/ntop/ntop.sh start` or die ($!);
and be done with it
No need to exit($cc)
Background the /usr/local/www/cgi-bin/ntop/ntop.sh start process depends on your script.

Hope this helps
 
Old 07-21-2005, 05:56 PM   #9
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
thanks a lot, I changed the script, but now I get this error from command line:
Code:
# perl ntop3.cgi
Content-Type: text/html; charset=ISO-8859-1

<html>
<head>
<title>ntop startup script</title>
<STYLE TYPE='text/css'>
h3 { color: red }
a { text-decoration: none; font: bold 14pt/16pt Ariel, serif }a:hover { color: red } /* when mouse is over link */</style>
</head>
<body bgcolor='#c0c0d0'>
<center><p><br></p><p><br></p><h1>Software error:</h1>
<pre>No such file or directory at ntop3.cgi line 22.</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message
and the time and date of the error.

</p>
[Fri Jul 22 00:54:44 2005] ntop3.cgi: No such file or directory at ntop3.cgi line 22.
 
Old 07-21-2005, 06:13 PM   #10
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
I changed the script a little bit.
this script works well from command line:
Code:
#!/usr/bin/perl -w

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

print $query->header;
print "<html>\n";
print "<head>\n";
print "<title>ntop startup script</title>\n";
print "<STYLE TYPE='text/css'>\n";
print "h3 { color: red }\n";
print "a { text-decoration: none; font: bold 14pt/16pt Ariel, serif }";
print "a:hover { color: red } /* when mouse is over link */";
print "</style>\n";
print "</head>\n";
print "<body bgcolor='#c0c0d0'>\n";
print "<center>";
print "<p><br></p>";
print "<p><br></p>";

my $bb = system('/usr/local/www/cgi-bin/ntop/ntop.sh stop' or die "cannot stop ntop: $!");
sleep(8);
$cc = system('/usr/local/www/cgi-bin/ntop/ntop.sh start &' or die "cannot start ntop: $!");
print "Status: ",$cc,"\n";

print "</body>";
print "</html>";

exit($cc);
but still cannot execute ntop via browser

in browser I get following output

Status: 0


in error log from Apache, I have:

[Thu Jul 22 00:20:54 2005] [error] [client 192.168.0.5] Permission denied

greetings
cccc

Last edited by cccc; 07-22-2005 at 07:21 AM.
 
Old 07-21-2005, 06:21 PM   #11
hlyrad
Member
 
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44

Rep: Reputation: 15
What is ntop.sh doing?
If it is a service that requires root privilages you may have to do a little more scripting.
 
Old 07-22-2005, 06:51 AM   #12
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Also, did you put the script in a cgi-bin directory as defined
with ScriptAlias in httpd.conf ?
 
Old 07-22-2005, 07:02 AM   #13
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
Yes, ntop.sh is in the same cgi-bin directory like ntop3.cgi perl script,
but still doesn't help.
 
Old 07-22-2005, 07:30 AM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Did you check ntop3.cgi permissions (is it executable) ?
Also the hlyrad's note stays valid, does ntop.sh require root privileges ?
 
Old 07-22-2005, 08:35 AM   #15
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
yes I did:

# chmod 755 ntop3.cgi

# chown nobody ntop3.cgi

or

# chown www ntop3.cgi


but this perl script still cannot start ntop.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
auditd outputting errors at service start & stop cdhgee Fedora 8 08-08-2005 01:22 PM
howto start a perl script only from tuesday to friday at 8.00 am ? cccc Linux - Newbie 4 03-19-2005 04:23 AM
Perl Script Program (need) : SpamAssassin-ClamAV-Procmail-Howto cyberjames Slackware 8 01-27-2005 11:53 AM
How to Call a C program from Perl CGI Script anoop_cn Programming 1 05-11-2004 04:37 PM
Start Program Script Cafrow Linux - Newbie 1 08-17-2003 04:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration