You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Alejandro Galue <ag...@sync.com.ve> on 2004/04/28 21:22:13 UTC
Problem with perl system() function
Hello All,
I´m using mod_perl2 and Apache2 over Fedora Core 1.
I have a handler that need to call an external program to complete the
request.
This program put its output to a file, and then I read this output from the
handler (after system function) to complete the request.
My problem is (I assume) when I call system, it returns inmediatly and of
course when I try to read the file output, it does not exist; becase it was
created later.
How can I force to wait for a system function terminate the execution of an
external program.
Thanks,
Alejandro Galue.
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Problem with perl system() function
Posted by Brian Reichert <re...@numachi.com>.
On Wed, Apr 28, 2004 at 03:55:23PM -0400, Alejandro Galue wrote:
>
> I tried this (inside the handler):
>
> ...
> my $cmd = '/opt/reports/bin/getdata';
> system($cmd, @params);
> if ( open(DATA, '/opt/reports/var/data.txt') ) {
> local $_;
> while (<DATA>) { $r->print($_) }
> close DATA;
> } else {
> $r->print("<h1>Can?t read data</h1>");
> }
> ...
>
> The program getdata put output in /opt/reports/var/data.txt
It may just be an example, but this handler doesn't create a unique
file; it may get stomped my multiple invokations.
Why not
open(DATA, join(' ', $cmd, @params, '|') || croak "$cmd failed: $? $!\n";
if you're trying to avoid generating a text file.
I wrote the System2 module to get STDERR and STDOUT returned in
scalars (and avoid the Bourne shell); see if that'd be useful for
you...
my ($out, $err) = system2(@args);
Are you checking on the exit status of your command?
my ($exit_value, $signal_num, $dumped_core) = &System2::exit_status($?)?
Good luck...
--
Brian Reichert <re...@numachi.com>
37 Crystal Ave. #303 Daytime number: (603) 434-6842
Derry NH 03038-1713 USA BSD admin/developer at large
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
RE: Problem with perl system() function
Posted by "Dr. Helmut Zeilinger" <hz...@hzlabs.de>.
Hi,
why don't you use "readpipe", which lets you avoid the intermediate data
file:
...
my $data = readpipe ($cmd);
...
Helmut
>
> I tried this (inside the handler):
>
> ...
> my $cmd = '/opt/reports/bin/getdata';
> system($cmd, @params);
> if ( open(DATA, '/opt/reports/var/data.txt') ) {
> local $_;
> while (<DATA>) { $r->print($_) }
> close DATA;
> } else {
> $r->print("<h1>Can´t read data</h1>");
> }
> ...
>
> The program getdata put output in /opt/reports/var/data.txt
>
> I tried also:
>
> qx{ $cmd @params }
>
> and
>
> `$cmd @params`
>
> but it does not work.
>
> The problem is that if you check the /opt/reports/var/ directory the
> data.txt is generated correctly for each requet.
>
> Thanks,
> Alejandro Galue.
>
> -----Original Message-----
> From: Tom Schindl [mailto:tomAtLinux@gmx.at]
> Sent: Wednesday, April 28, 2004 3:29 PM
> To: Alejandro Galue
> Cc: modperl@perl.apache.org
> Subject: Re: Problem with perl system() function
>
> Hi,
>
> system will wait until the executed shell-script has finished. I'd rather
> guess that you are not allowed to call the commandline programm you'd like
> to call.
>
> Did you get any thing in the error_log? How do you call the
> system-command:
>
> * system
> *``
> * qx
>
> Maybe you could show us the code you're using and we'll tell you whats
> wrong there.
>
> Tom
>
> Alejandro Galue wrote:
>> Hello All,
>>
>> I´m using mod_perl2 and Apache2 over Fedora Core 1.
>>
>> I have a handler that need to call an external program to complete the
>> request.
>>
>> This program put its output to a file, and then I read this output
>> from the handler (after system function) to complete the request.
>>
>> My problem is (I assume) when I call system, it returns inmediatly and
>> of course when I try to read the file output, it does not exist;
>> becase it was created later.
>>
>> How can I force to wait for a system function terminate the execution
>> of an external program.
>>
>> Thanks,
>> Alejandro Galue.
>>
>>
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
RE: Problem with perl system() function
Posted by Alejandro Galue <ag...@sync.com.ve>.
I tried this (inside the handler):
...
my $cmd = '/opt/reports/bin/getdata';
system($cmd, @params);
if ( open(DATA, '/opt/reports/var/data.txt') ) {
local $_;
while (<DATA>) { $r->print($_) }
close DATA;
} else {
$r->print("<h1>Can´t read data</h1>");
}
...
The program getdata put output in /opt/reports/var/data.txt
I tried also:
qx{ $cmd @params }
and
`$cmd @params`
but it does not work.
The problem is that if you check the /opt/reports/var/ directory the
data.txt is generated correctly for each requet.
Thanks,
Alejandro Galue.
-----Original Message-----
From: Tom Schindl [mailto:tomAtLinux@gmx.at]
Sent: Wednesday, April 28, 2004 3:29 PM
To: Alejandro Galue
Cc: modperl@perl.apache.org
Subject: Re: Problem with perl system() function
Hi,
system will wait until the executed shell-script has finished. I'd rather
guess that you are not allowed to call the commandline programm you'd like
to call.
Did you get any thing in the error_log? How do you call the system-command:
* system
*``
* qx
Maybe you could show us the code you're using and we'll tell you whats wrong
there.
Tom
Alejandro Galue wrote:
> Hello All,
>
> I´m using mod_perl2 and Apache2 over Fedora Core 1.
>
> I have a handler that need to call an external program to complete the
> request.
>
> This program put its output to a file, and then I read this output
> from the handler (after system function) to complete the request.
>
> My problem is (I assume) when I call system, it returns inmediatly and
> of course when I try to read the file output, it does not exist;
> becase it was created later.
>
> How can I force to wait for a system function terminate the execution
> of an external program.
>
> Thanks,
> Alejandro Galue.
>
>
>
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Problem with perl system() function
Posted by Tom Schindl <to...@gmx.at>.
Hi,
system will wait until the executed shell-script has finished. I'd
rather guess that you are not allowed to call the commandline programm
you'd like to call.
Did you get any thing in the error_log? How do you call the system-command:
* system
*``
* qx
Maybe you could show us the code you're using and we'll tell you whats
wrong there.
Tom
Alejandro Galue wrote:
> Hello All,
>
> I´m using mod_perl2 and Apache2 over Fedora Core 1.
>
> I have a handler that need to call an external program to complete the
> request.
>
> This program put its output to a file, and then I read this output from the
> handler (after system function) to complete the request.
>
> My problem is (I assume) when I call system, it returns inmediatly and of
> course when I try to read the file output, it does not exist; becase it was
> created later.
>
> How can I force to wait for a system function terminate the execution of an
> external program.
>
> Thanks,
> Alejandro Galue.
>
>
>
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Problem with perl system() function
Posted by ydnar <yd...@shaderlab.com>.
You could use IPC::Run, which lets you pipe data to an executable, then
read the result back:
use strict;
use IPC::Run qw(start finish pump);
sub handler
{
my $r = shift;
my $html = qw
{
<html>
<body>
<h1>Foo</h1>
Bar<br>
Baz
</body>
</html>
};
# command
my $cmd = [ "/path/to/htmldoc",
qw(
--quiet --jpeg --webpage -t pdf
--bodyfont Helvetica --fontsize 7
--browserwidth 1024
--left 16 --top 16 --right 16 --bottom 16
--header ... --footer ...
-
)];
# run htmldoc subprocess
my $output;
my $handle = start( $cmd, \$html, \$output, ) || die "Unable to open
htmldoc: $?";
pump $handle while( length $html );
finish $handle;
# set the filename
$fname =~ s/ /_/g;
$lname =~ s/ /_/g;
# read pdf back in and send to browser
#print header( -content_type => "application/pdf",
print header( -content_type => "application/pdf",
-content_disposition => qq{attachment; filename="foo.pdf"} );
print $output;
}
Alejandro Galue wrote:
>Hello All,
>
>I´m using mod_perl2 and Apache2 over Fedora Core 1.
>
>I have a handler that need to call an external program to complete the
>request.
>
>This program put its output to a file, and then I read this output from the
>handler (after system function) to complete the request.
>
>My problem is (I assume) when I call system, it returns inmediatly and of
>course when I try to read the file output, it does not exist; becase it was
>created later.
>
>How can I force to wait for a system function terminate the execution of an
>external program.
>
>Thanks,
>Alejandro Galue.
>
>
>
>
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: Problem with perl system() function
Posted by Stas Bekman <st...@stason.org>.
Alejandro Galue wrote:
> Hello All,
>
> I´m using mod_perl2 and Apache2 over Fedora Core 1.
>
> I have a handler that need to call an external program to complete the
> request.
>
> This program put its output to a file, and then I read this output from the
> handler (after system function) to complete the request.
>
> My problem is (I assume) when I call system, it returns inmediatly and of
> course when I try to read the file output, it does not exist; becase it was
> created later.
>
> How can I force to wait for a system function terminate the execution of an
> external program.
Sounds like a generic perl question, not really specific to mod_perl. Take a
look at IPC::Run or IPC::Run3. Hopefully you will find a better place to
discuss this, like perlmonks.org. Thanks.
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html