maslofer has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks,

I have been trying to run a command line program from a perl CGI script.

The following attempts all work if I run the script from the commandline, but fail if I access it through the web (they return nothing (some kind of timeout maybe??))

1.
print `/opt/csw/bin/octave -qfH < $filename`;
2.
open (OCTAVE, "| /opt/csw/bin/octave -qfH < $filename") or die "Can't +open pipe: $!\n"; while(<OCTAVE>){ print $_; } close(OCTAVE) or die "Couldn't close octave: $!";

The latter script print the "Couldn't close..." error message when run from the web server.

Once again they work fine when run from the command line.

What is going on monks, I am completly befuddled.

-maslofer

Replies are listed 'Best First'.
Re: Running Command Line Programs from a CGI Script
by Solo (Deacon) on Jun 10, 2005 at 19:25 UTC
    If you can, use CGI::Carp qw/ fatalsToBrowser /;. You may also see more if you redirect STDERR to STDOUT.

    print `/opt/csw/bin/octave -qfH < $filename 2>&1`;

    --Solo

    --
    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
Re: Running Command Line Programs from a CGI Script
by cool_jr256 (Acolyte) on Jun 10, 2005 at 19:08 UTC
    I'd check the weblogs, maybe you'll find a bit more info in there. I do also suspect permissions issues but then again I've seen weirder things....
    you can also try running it this way...
    $output=`/opt/csw/bin/octave -qfH < $filename`;
Re: Running Command Line Programs from a CGI Script
by etm117 (Pilgrim) on Jun 10, 2005 at 18:44 UTC
    My first instinct would be to check whether the user the webserver is running under has the same permissions as the login you are using. If not, it may not have permissions to do what you are trying to.
      Actually if I input a simple octave script, it runs fine on the server. It is just when I input a long-running script that it returns nothing, leading me to the idea of timeout issues.

      Unfortunately I can't find docs about timeouts in this scenario anywhere.
        What kind of server are you running and how long is "long-running"? I think the default timeout for Apache is fairly long, maybe 5 minutes?
Re: Running Command Line Programs from a CGI Script
by mda2 (Hermit) on Jun 13, 2005 at 02:00 UTC
    Some years ago I had a similar problem with random sendmail problems. To solve it I tried put header first, but the response is redirect stderr. (Previous sugest by Solo)

    Some time after I think again about and concludes with separated buffers handled by apache web server...

    When your command-line binary end, flush STDERR, before your STDOUT flushed by your cgi.

    Today I like to put three lines on start my codes...

  • use strict;
  • use warnings; (or -w at shebang line)
  • $|++; # (Update: auto flush handles, like autoflush call on IO::Handle)

    --
    Marco Antonio
    Rio-PM