I need a perl script to stop & start program

Need help with C, C++, perl, python, etc?

I need a perl script to stop & start program

Postby cc » 2005-07-21 00:11

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
cc
cc
 
Posts: 820
Joined: 2005-06-08 19:14

Postby lacek » 2005-07-21 10:57

Use the "system" function. You need to check the return value this way (this is a copy-paste from the 'perlfunc' manual page, which is your friend):
Code: Select all
system("sh /home/ntop.sh stop")
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;
}


However, you can register shell scripts as cgi scripts, so you can write your program in shell script as well...
lacek
Moderator Team Member
 
Posts: 769
Joined: 2004-03-11 18:49
Location: Budapest, Hungary

Postby cc » 2005-07-21 22:34

thanks

I've written this script:
Code: Select all
#!/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 "</head>\n";
print "<body bgcolor='#c0c0d0'>\n";
print "<center>";
print "<p><br></p>";
print "<p><br></p>";

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

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


from command line it works well, but via browser I get:
Status: 0 ntop

in error log from apache I have:

[Thu Jul 21 22:50:54 2005] [error] [client 192.168.0.105] Permission denied

howto change rights to execute system "program" via browser ?
Last edited by cc on 2005-07-27 01:02, edited 5 times in total.
cc
 
Posts: 820
Joined: 2005-06-08 19:14

Postby lacek » 2005-07-22 11:55

The web services are run by the www-data user. You need to make sure that this user has all the necessary permissions.
lacek
Moderator Team Member
 
Posts: 769
Joined: 2004-03-11 18:49
Location: Budapest, Hungary


Return to Programming

Who is online

Users browsing this forum: No registered users and 7 guests

fashionable