Dear Perl Monk,
A few days ago I posted a question "close STDOUT does not work, why?" at
close STDOUT does not work, why?
and BrowserUK pointed out that I needed to close STDIN and STDERR as well and it
made the redirection worked. However, when I tried to invoke an external program
using PIPE later in my program, it failed. Below is a piece of code I wrote to
demonstrate my problem. Any thoughts?
#!/usr/bin/perl
use CGI qw(:standard);
$| = 1;
if (my $pid = fork) { #parent does
close STDOUT;
close STDIN;
close STDERR;
open STDIN, '<', '/dev/null';
open STDOUT, '>', '/dev/null';
my $err_file = "/var/www/html/cgi_data/error_log";
open STDERR, '>', $err_file;
start_x();
}
else
{
print header(), q~
<HTML>
<HEAD>
<META HTTP-EQUIV="Refresh" CONTENT="3; URL=/cgi_data/page1.html">
<TITLE>New Site notification</TITLE>
</HEAD>
<BODY>My homepage has moved to a new location. I am here</BODY></HTML>
~;
exit;
}
sub start_x
{
$com = "./myprog 100";
open PIPE, "$com |";
$outfile1 = "../html/cgi_data/page2.html";
open OUT, ">$outfile1" or die "Can not open for write: $outfile";
while ($data = <PIPE>)
{
print OUT "$data\n";
}
close OUT;
}
The problem occurs at
open PIPE, "$com |";
I tried to put a print STDERR "I am here"; and noticed it does print to the
error_log file if I put the print statement above the open PIPE but does not if
I put the print statement after it. Any thoughts?
Thanks.
Code tags added by GrandFather
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.