#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; my $pid = $$; my $pid1 = open3(0, \*READ,\*ERROR,"top -d 1 -b -p $pid "); #if \*ERROR is false, STDERR is sent to STDOUT # add more commands here if you want, but make sure the # filehandles are different. It would be an improvement to use # IO::Handle for the filehandles my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ foreach my $h ($sel->can_read){ my $buf = ''; if ($h eq \*ERROR){ sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} }else{ sysread(READ,$buf,4096); if($buf){print "$buf\n"} } } }