open (RES, "/usr/bin/top -bs -n 1 |");

S

Sandrine CHEN

Thank you to tell me why:

I have one program like following, it could display results on HTML
page on one machine, but not on another which has the same kernel of
2.4.18-3, Redhat Linux, I star to wonder is it that i haven't written
the program in a way more popular, or there is other point that i need
to check or take care?

What's more, on the same machine(that result couldn't display), if i
run the script manually under command line, it couldn't display
results. :( Why!

____________________________________________
#!/usr/bin/perl -w

print "Content-type: text/html\n\n";
print "<html>\n";
print "<test of result>";
print "<body>";

open (RES, "/usr/bin/top -bs -n 1 |");
while (<RES>) {
print $_,"<br>";
}
close RES;

print "</body>";
____________________________________________

thank u very much...
print "</html>";
 
A

Anno Siegel

Sandrine CHEN said:
Thank you to tell me why:

I have one program like following, it could display results on HTML
page on one machine, but not on another which has the same kernel of
2.4.18-3, Redhat Linux, I star to wonder is it that i haven't written
the program in a way more popular, or there is other point that i need
to check or take care?

What's more, on the same machine(that result couldn't display), if i
run the script manually under command line, it couldn't display
results. :( Why!

So obviously there is a difference between the two machines.

What does it mean when you say "couldn't display results"? Does the
program show no output at all? Does it show part of the expected
output, and if so, where does it stop. Does the program finish normally,
finish with a return code, or not finish at all (hang)?

Without you telling us more, there is no chance of guessing what the
difference may be.
____________________________________________
#!/usr/bin/perl -w

print "Content-type: text/html\n\n";
print "<html>\n";
print "<test of result>";
print "<body>";

open (RES, "/usr/bin/top -bs -n 1 |");

You should check the return value of open(). That may give you
a first hint.
while (<RES>) {
print $_,"<br>";
}
close RES;

print "</body>";
____________________________________________

thank u very much...
print "</html>";

Anno
 
S

Steve Grazzini

Sandrine CHEN said:
open (RES, "/usr/bin/top -bs -n 1 |");
while (<RES>) {
print $_,"<br>";
}
close RES;

The error-checking for piped open works like this:

#
# open can also fail on fork(), pipe() or exec()
#

open my $top, '-|', qw(/usr/bin/top -bs -n 1)
or die "Couldn't start top: $!";

print while <$top>;

#
# close also fails if wait() yields nonzero exit status
#

close($top) or die "close: ", $! || $?;

Without any error checks, of course you don't know what went wrong.
If the open() fails, then perl will give you the right error. If
"top" is failing and you want to capture the diagnostic (i.e. not just
the wait() status) use IPC::Open3 to catch its stderr.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top