#!/usr/bin/perl use strict; use warnings; open(STAT, '-|', 'vmstat 2') or die "Could not open pipe: $!"; my $count; # not necessary, just for demonstration while (){ last if $count++ > 5; # we don't want more than 5 lines of output print $_, "\n"; } print "...done\n";