#!/usr/bin/perl use strict; use warnings; use IPC::Open2; use IO::Select; # for select #my $cmd = "sqlplus -S apps/******"; # This is the query I really want # to run my $cmd = "ssh oraprod\@remote_server './x 100'"; # "x" executes # sleep $1 to # simulate a process # hunging on the # remote server my $timeout=1; my $infh; my $outfh; my @output; my $pid; eval{ $pid = open2($outfh, $infh, $cmd); }; die "open2: $@\n" if $@; print "PID was $pid\n"; #-------------------------- DEBUG ------------ #print "Timeout was $timeout\n"; my $sel = new IO::Select; # create a select object to notify # us on write ready. $sel->add($infh); # add the std input file handle if ($sel->can_write($timeout)){ # I will base my "DB alive" test off of this query's output #print $infh "set feedback off pages 0\nselect sysdate from dual;\n"; } else { die "Timed out while waiting for the \"Write OK\" to send the query\n"; } print "PID was $pid\n"; #-------------------------- DEBUG ------------ $sel->add($outfh); # add the std output file handle while ($sel->can_read($timeout)) { # Returns the std output file # handle even while the sleep is # being x. # Would it do the same if "x" were # a truly hung process? # My hope is it won't so the # while's condition would be false # after timeout causing the script # to skip the while. chomp(my $line=<$outfh>); # There is nothing on "x" std output to # be read so $line does not get defined. push(@output,$line); } close $infh; print "PID was $pid\n"; #-------------------------- DEBUG ------------ waitpid $pid,0; $sel->remove($infh,$outfh); # remove std output f handle from the list unless (@output) { die "Timed out while waiting for query output\n"; } print "$_\n" for @output;