top -bcS -n 1 -b batch mode (non-interactive) -n iteration count -S cumulative CPU for dead children -c display full command line #### lll.pl: sub longestLineLength { my $longestLen = undef; for my $l ( @_ ) { if ( length($l) > $longestLen ) { $longestLen = length($l); } } return $longestLen; } test-01.pl: #!/usr/bin/perl require "lll.pl"; open (TOP, "/usr/bin/top -bcS -n 1 |") || die "open pipe from top failed $!"; print longestLineLength( ) . "\n"; $ ./test-01.pl 80 #### $ COLUMNS=1000 ./test-01.pl 552 #### open (TOP, "COLUMNS=1000 /usr/bin/top -bcS -n 1 |") OR open (TOP, "/usr/bin/env -i COLUMNS=1000 /usr/bin/top -bcS -n 1 |") #### $ ./test-02.pl 552 #### $ ./test-02.pl 2>err 81 #### test-03.pl: open (TOP, "/usr/bin/env -i COLUMNS=1000 /usr/bin/top -bcS -n 1 2>err.txt |") $ ./test-03.pl 81 #### #!/usr/bin/perl require "lll.pl"; my $outFileName = shift @ARGV; my $errFileName = $outFileName . ".err"; open( STDOUT, ">> $outFileName" ) || die "failed to redirect STDOUT to '$outFileName': $!"; open( STDERR, ">> $errFileName" ) || die "failed to redirect STDERR to '$errFileName': $!"; open (TOP, "/usr/bin/env -i COLUMNS=1000 /usr/bin/top -bcS -n 1 |") || die "open pipe from top failed $!"; print longestLineLength( ) . "\n"; $ ./test-04.pl output $ cat output 81 #### $ perl -v This is perl, v5.6.1 built for i386-linux $ type -a top top is /usr/bin/top $ rpm -qf /usr/bin/top procps-2.0.7-11 $ ls -l /bin/sh ... /bin/sh -> bash $ rpm -qf /bin/bash /bin/sh bash-2.05-8 bash-2.05-8 $ uname -a Linux ahost 2.4.9-e.40enterprise #1 SMP Thu Apr 8 16:43:01 EDT 2004 i686 unknown (actually it's RedHat Enterprise Linux 2.1, w/ recent patches)