Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Running an external script from a Perl CGI script

by Eureka_sg (Monk)
on Feb 21, 2001 at 16:28 UTC ( [id://59908]=perlquestion: print w/replies, xml ) Need Help??

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

I have a CGI script that uses the parameter passed to it as an argument for another perl script,xx.pl. However, I was unable to run xx.pl when running from the web.I did something like this:

<P>open(A,"xx.pl $value|"); while(<A>){ $results .= $_;}</P>

when i run it from the command line, there is no probl. but it doesnt work when accessing from the web

After troubleshooting, I've found out that:
1) the parameter,$value is being passed correctly
2) xx.pl is accessible to all, chmod 777 xx.pl
3) when I replaced xx.pl with a simple command, ls , it works perfectly
4) when I put 'ls' into a shell script and use it to replace xx.pl, it doesnt work again

I think I'm missing out a crucial point somewhere. Can anyone offer me some insight? Thanks

Replies are listed 'Best First'.
Re: Running an external script from a Perl CGI script
by BlueLines (Hermit) on Feb 21, 2001 at 16:55 UTC
    1) I think it may be a path issue. Check the absoute pathname and try it again (ls is a shell builtin and requires no path).

    2) Try calling the external script with system and check the return value. Then try calling it with backticks.

    3) Try running something like this from the cgi:
    foreach my $key (keys %ENVY) { print "$key: $ENV{$key}\n"; }

    look at the output. Is there anything weird there that would screw with shell processes?

    4) Also, make sure that there aren't any zombie processes of apache hanging on. If there are, try running the script and redirecting the output to /dev/null. I've had weird issues with apache zombying off httpd processes because a called cgi program forked off another shell for the process. Even though the process finished fine, the second process seemed to never let go of the shell (even though it did). We observed this at my workplace with a cgi that started mysql. The supplied mysql.server script seems to break Apache + mod_perl when you call it from a cgi. We finally solved the problem by calling mysql.server and redirecting STDERR and STDOUT to /dev/null.

    BlueLines

    Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.
Re: Running an external script from a Perl CGI script
by dash2 (Hermit) on Feb 21, 2001 at 17:58 UTC
    Perhaps you have a good reason for this, but wouldn't it be simpler to "require" the perl script and then call a function in it with the parameter, rather than passing it to the shell and back?

    dave

(ar0n) Re: Running an external script from a Perl CGI script
by ar0n (Priest) on Feb 21, 2001 at 18:16 UTC
    Listen to your computer. Can you hear it humming? It's working for you.

    That said, take a peek at your webserver logs. What do they say?

    [ar0n]

(jeffa) Re: Running an external script from a Perl CGI script
by jeffa (Bishop) on Feb 21, 2001 at 21:14 UTC
    Do what ar0n says - check that log file!

    Of course, that really won't do you much good unless you change your open line to something like:

    open(A, "xx.pl $value|") or print STDERR "oops: $!\n";
    tail -f /usr/local/apache/logs/error_log is your friend. :)

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Running an external script from a Perl CGI script
by Jouke (Curate) on Feb 21, 2001 at 16:39 UTC
    Maybe you're not running it in the directory you think. Try specifying the complete path to the script...

    Jouke Visser, Perl 'Adept'
      Thanks,but I've tried including the complete path as well
Re: Running an external script from a Perl CGI script
by cat2014 (Monk) on Feb 21, 2001 at 23:44 UTC
    Most likely, the xx.pl script is getting killed off when the cgi exits. Do this:
    system "/the/full/path/to/xx.pl @arguments $another_argument </dev/nu +ll >/full/path/to/output.log 2>&1 &";
    (that log at the end can be switched to /dev/null, too, if you don't want to look at the output anymore. but i find it really useful for debugging.)

    I'd guess that the reason that ls works but your xx.pl doesn't is that ls can complete quickly enough, but xx.pl takes longer & isn't done running when the cgi exits & its child processes are killed off. i got bitten by this on one of the first CGIs i wrote, and it took a friend & i a while of digging through FAQs to uncover the trick to keeping the child process running. -- cat

      Thanks everyone for their suggestions. Its due to the display of html(and thus $result="") before the child process is completed as what cat2014 suggested.

Re: Running an external script from a Perl CGI script
by Maclir (Curate) on Feb 22, 2001 at 06:34 UTC
    Check the environment your CGI programs run under. Generally, if you are running apache, the user:group is nobody:nobody. As a test, print out the contents of your %ENV hash from a cgi program - maybe the PATH is different, and it cannot find your perl binary?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://59908]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-23 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found