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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
07-20-2005, 07:28 PM
|
#1
|
|
Senior Member
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,544
Rep:
|
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
|
|
|
|
07-21-2005, 12:18 PM
|
#2
|
|
Member
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44
Rep:
|
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>";
|
|
|
|
07-21-2005, 02:50 PM
|
#3
|
|
Senior Member
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,544
Original Poster
Rep:
|
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
|
|
|
|
07-21-2005, 02:55 PM
|
#4
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
Are you sure ntop start from your script in command-line ?
Wasn't it already started ?
Normally it should exit after the exit($bb);
|
|
|
|
07-21-2005, 03:07 PM
|
#5
|
|
Senior Member
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,544
Original Poster
Rep:
|
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.
|
|
|
|
07-21-2005, 03:59 PM
|
#6
|
|
Member
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44
Rep:
|
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.
|
|
|
|
07-21-2005, 04:22 PM
|
#7
|
|
Senior Member
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,544
Original Poster
Rep:
|
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.
|
|
|
|
07-21-2005, 05:33 PM
|
#8
|
|
Member
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44
Rep:
|
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
|
|
|
|
07-21-2005, 05:56 PM
|
#9
|
|
Senior Member
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,544
Original Poster
Rep:
|
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.
|
|
|
|
07-21-2005, 06:13 PM
|
#10
|
|
Senior Member
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,544
Original Poster
Rep:
|
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.
|
|
|
|
07-21-2005, 06:21 PM
|
#11
|
|
Member
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44
Rep:
|
What is ntop.sh doing?
If it is a service that requires root privilages you may have to do a little more scripting.
|
|
|
|
07-22-2005, 06:51 AM
|
#12
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
Also, did you put the script in a cgi-bin directory as defined
with ScriptAlias in httpd.conf ?
|
|
|
|
07-22-2005, 07:02 AM
|
#13
|
|
Senior Member
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,544
Original Poster
Rep:
|
Yes, ntop.sh is in the same cgi-bin directory like ntop3.cgi perl script,
but still doesn't help.
|
|
|
|
07-22-2005, 07:30 AM
|
#14
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
Did you check ntop3.cgi permissions (is it executable) ?
Also the hlyrad's note stays valid, does ntop.sh require root privileges ?
|
|
|
|
07-22-2005, 08:35 AM
|
#15
|
|
Senior Member
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,544
Original Poster
Rep:
|
yes I did:
# chmod 755 ntop3.cgi
# chown nobody ntop3.cgi
or
# chown www ntop3.cgi
but this perl script still cannot start ntop.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:12 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|