简体   繁体   中英

System command not running when called through cgi from Perl

I have been stuck for many days where I have a perl script. I have made a function in my CGI File which has some HTML content and a system command to execute a shell script.Then I have an href which calls the perl file which in turn calls the CGI function and shows the content on the browser. But everything is loading except the execution of system command in CGI. I tried to put this system command in my Perl File itself and on execution it is successfully executing the shell script from command line. But I wanted this to happen on a button click so I had to make a CGI call but still I am not able to call it. I have been through many questions but I still am looking for the perfect answer. Here is my code: test1.pl

#!/usr/bin/perl -w

use strict;

use CGI::Carp qw(fatalsToBrowser);
use Basecamp::UI;
use Basecamp::Dashboard;
use Basecamp::Info;

my $cgi = Basecamp::UI->new->cgi();
my $dashboard = Basecamp::Dashboard->new();
my @html = $cgi->tab_ext();

#push @html, '<div class="page">';
#push @html, newstuff();
#push @html, '</div>';
#print "Hello Hey";
#system( ' sh  /home/basecamplocal/Perforce/depot/qeutils/basecamp_dev/sample.sh ');

print $cgi->begin('Basecamp | ext', $dashboard), $cgi->begin_page();
print @html;
print $cgi->end_page(), $cgi->begin_footer(), $cgi->end_footer(), $cgi->end();

shell script is:

#!/bin/bash
#perl Basecamp.pl > log_sample.txt
touch log_sample.txt

echo "Good Day"

CGI File Function:

=item tab_ext()
======================================================================================

        DESC:   Generates appropriate tabs for About, about.pl.
        OUT:    Returns array of scalars with html to draw the tabs and any associated
                        subtabs.

======================================================================================
=cut
sub tab_ext
{
        my ($self) = @_;
        my $tab = $self->param('tab');
        my @tabs = ('<li><a href="test1.pl?tab=New">Status Restart</a></li>');
        my @subtabs = ();
        my @html = ();

        $tabs[0] = '<li class="selected"><a href="#">Status Restart</a></li>';

        @html = ('<ul id="tabs">', @tabs, '</ul>');

        push @html, ('<ul id="subtabs">', @subtabs, '</ul>');
        print "Content-type: text/html\n\n";
        system("sh /full path to shell file /sample.sh ");


        return @html;

}

and I am calling the test1.pl file on a href call html.The whole html content from CGI is being displayed except the execution of shell script. A helping hand would be appreciated.

Looks like you have permission issue. First check the permission of your system.sh program and parent directories. I think web user has no permission to execute the program.

Test code : test.cgi

Persmission : 755

#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print header( -type => 'text/html' );
print <<EOF;

<title>Browser Page</title>
<body>
<div> Hellow </div>

EOF

my $command ="sh /tmp/abc.sh";
print "$command<br>";
system($command);
print "</body>";

code: abc.sh

#!/bin/bash

echo "Good Day"

Output on the browser is

Hellow
sh /tmp/abc.sh 
Good Day

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM