In batch mode, one iteration of top won't display the information that you are interested in. Add the
-d2 option to run a couple of iterations, and the second should include the CPU info.
#!/usr/bin/perl
use warnings;
use strict;
open (PIPE, "/usr/local/bin/top -b -d2 |") or die "Cannot open top: $!
+";
while (<PIPE>) {
if (/^CPU/) {
print;
last;
}
}
close(PIPE);
exit(0);
I tested this on a Sun-Fire-280 running Solaris 9 and I am
not a Unix guru so your mileage may vary. Best of luck.
Updated: removed temporary variable for input line.