| Charles DeRykus 2006-10-30, 7:09 pm |
| inetquestion wrote:
> I can't seem to get rid of the error message if sar doesn't exist in
> the path. The redirection of stderr from within the backticks isn't
> working via perl. Is there another way to allieviate these errors from
> showing up in the console? Any suggestions on how to get cpuidle which
> will work on unix or windows?
>
> $CpuAvg=`sar -u 15 2>/dev/null | tail -1 | awk '{print \$5}'`;
> if ( $CpuAvg=~/[0-9]*/ ) {
> $CpuAvg=`vmstat 15 2 | tail -1 | awk '{print \$22}'`;
> }
> chomp($CpuAvg);
>
On Unix, STDERR can be redirected but you'd have to pry out any shell
errors because the output would be interleaved. You could also exec
2>/dev/null but I can't imagine that being helpful.
$CpuAvg = `exec 2>&1; sar -u 15 | tail -1 | awk '{print \$22}'`;
IPC::Open3 is another Unix possibility although I don't know how
well it works on other OS's such as Win32:
use IPC::Open3;
# make errors available from /path/to/errfile
open(my $e, ">","/path/to/errfile") or die $!;
$p = open3($w, $r, ">&" . fileno($e), "sar -u 15...." );
....
hth,
--
Charles DeRykus
|