in reply to Executing another script from a CGI

Sound like a path problem. Remember, your web scripts probably run as a different user (nobody) who may or may not have the same path as you. Try using fullpaths and using the '-w' flag:

my $output = system "/path/to/perl -w /path/to/script -opts";

-derby

Replies are listed 'Best First'.
Re: Re: Executing another script from a CGI
by higle (Chaplain) on Jul 24, 2002 at 20:32 UTC
    Thanks!

    Wow, I didn't even think to check that. I thought we had our box configured with Perl in the path for CGI processes, but I guess not.

    My philosophy has always been, "Check the simplest things first." Looks like I should follow my own advice...

      higle
      ...correct me if I'm wrong, but you still may have a problem.

      Supplying the full path will get this additional script to run, but I don't think you're going to get the output from the command by grabbing the return value from system().

      What you'll get stored in $output will simply be the exit code from your other script.

      So here's how you can execute the external script and grab the output into a variable:

      open (PROC "/path/to/perl myscript.pl |") || die "horribly"; @lines = <PROC>; close PROC;

      You may want to add other civilities, such as join()'ing the lines of output into one big scalar if that suits you. But this is one good way of grabbing output from a running process. Best of luck.

      ---v

        Yeah, that was the next problem. At first I was using the backticks, but the problem was that the utility script took 20 or so minutes to run, and the backtick operator only returns after the child process has stopped running, so the operation timed out from the browser. I of course was using a nohup to keep the utility script running on the server, but the end user would see it as an error...

        I implemented the open method after that, and it works beautifully :)

        Final, working code (minus sensitive info):

        ...........................
        #!/opt/perl5/bin/perl -w use strict; use CGI; # set path, instead of putting it in the open statement $ENV{'PATH'} = "/usr/bin:/opt/perl5/bin/:"; #location of product_gen.pl, plus arguments my $gen_script = "/path/to/server/product_gen.pl -prfv"; # Create a new CGI object my $cgi = new CGI; # Output HTML header to browser print $cgi->header('text/html'); print qq{<h4>script executing</h4>}; open(PRODUCT_GEN, "nohup perl $gen_script |") or die "Error: $!\n"; while (<PRODUCT_GEN>) { print $_, "<br>"; } close(PRODUCT_GEN);
        ...........................

        Thanks again for all the assistance!

          higle
        True ... true about not getting the output of the command, but it will get the return code (system) with some work. If you want capture STDOUT, you could also use the backticks (as described in th qx/STRING/ section of perlop).

        -derby