http://www.perlmonks.org?node_id=1010472


in reply to CGI Script Working fine from my command prompt but Shows Internal server error when called through browser

My CGI script:
#!/ms/dist/perl5/bin/perl5.8 -w BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser'); } use strict; use warnings; use CGI::Carp qw/fatalsToBrowser/; use Env; use lib; use CGI qw( :html3 ); use CGI::Carp qw(fatalsToBrowser); my $q = CGI->new; print $q->header(); print "Hello, world!"; my $stat = `autostatus -J aa-test-bx-dly`; print "\nHI $stat\n"; open(FH1,">/tmp/ac.txt") or die "Cannot create file"; close(FH1); chmod 0777, "/tmp/ac.txt"; open(DAT,">>/tmp/ac.txt"); print DAT "TEST JOB aa-test-bx-dly $stat"; close(DAT); open(CATFILE,"/tmp/ac.txt") || die("Cannot Open File"); my(@catfile) = <CATFILE>; close CATFILE; print "\n\n$catfile[0]\n\n";
My HTML script:
print "content-type: text/html \n\n"; <FORM action="../../cgi-bin/test_1.cgi" method="POST"> First Name: <input type="text" name="first_name"> <br> Last Name: <input type="text" name="last_name"> <input type="submit" value="Submit"> </FORM>

The code shows status when run on command prompt but doesnot show the status of job when run on browser.I am putting it in file , but still it has correct status in file, but when on browser it loses status

Please suggest.

Replies are listed 'Best First'.
Re: Help required for CGI Script
by Corion (Patriarch) on Dec 27, 2012 at 09:39 UTC

    Likely, the user your CGI script runs as is not the user you are using from the command line. Or maybe the web server user does not have access to the autostatus program because $ENV{PATH} is set up differently under your web server than it is for your command prompt. You never check the result/error status of your backtick call.

    As a first and easy try, I would attempt to give the full path to autostatus instead of hoping that $ENV{PATH} is set up as it is for your shell account.

      I have given the full path , still that doesnot work.

        You will need to do more debugging. Find out what user your CGI gets run as. Find out what the error code is when you try to launch the autostat program. Find out whether the launched program outputs an error message. Such an error message could end up in the web server error log for example. Compare the rest of the environment of the CGI script to the environment of the shell.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Help required for CGI Script
by muba (Priest) on Dec 27, 2012 at 09:38 UTC
    my $stat = `autostatus -J aa-test-bx-dly`;

    Perhaps autostatus doesn't live in $PATH for whichever user the CGI process runs as.

      So what should i do

        Specify the full path to the program. /usr/bin/autostatus or whichever.

Re: Help required for CGI Script
by graff (Chancellor) on Dec 27, 2012 at 17:41 UTC
    When you run it via a browser, does your script return "Hello, World!" to the browser, and does the file "/tmp/ac.txt" contain what you expected? Do you see the word "HI"? What does that file contain (what is the expected output from "autostatus -J aa-test-bx-dly")?

    Since you are using "fatalsToBrowser", are you seeing any error messages? What do they say?

    Your code only tries to print the first line of "/tmp/ac.txt" to the browser, but if the output begins with a blank line, that's all your program will send to the browser.

    If the autostatus output contains angle brackets, then your browser will be trying to interpret it as HTML tags, and will probably not display any of it (because the data can't be parsed that way). In order to see the "autostatus" output presented, you may have to print a <pre> tag before printing the command output, and then print a </pre> tag afterwards.

      The output expected is RUNNING or SUCCESS .. Also the file so created doesnt have any blank lines. Please guide me the solution. Thanks
        The solution is that you debug your program using the advice and links already provided in reply to your several posts. If you're unable or unwilling to do so consider speaking to your manager or team leader explaining the situation. Consider the code you've actually written, what it does and what you're attempting to do. Your "expected output" does not occur within your program. http://learn.perl.org, tutorials.
        Also the output is

        Content-Type: text/html; charset=ISO-8859-1 Hello, world! HI TEST JOB aa-test-bx-dly