[Canberra-pm] How to exit a hung pipe to a shell command [SEC=UNCLASSIFIED]

Jepri jepri at alphacomplex.org
Tue Nov 27 09:30:07 PST 2007


  while (($results = <ZPING>) || (sleep(60) != 60) )

if ($results = <ZPING>) fails, you will sleep a further 60 seconds due 
to (sleep(60) != 60).

Under unix you should get a "read failed because pipe closed" type error 
when zping exits.  If zping doesn't exit you can try either the alarm 
function that nick mentioned (you might have to muck around with eval or 
handlers to make that work), or try non-blocking reads on your process 
handle.

I like non-blocking reads, but they are poor for efficiency and so 
considered naughty.  I'll let you read up on the non-blocking reads, 
it's fairly complex.

Sorry about the delayed reply, my mail server went wrong in many ways.

John.Hockaday at ga.gov.au wrote:
> Hi All,
> 
> I have a PERL pipe that calls a c++ binary called
> "/public/data/asddgate/isite/current/bin/zping".  It tries to ping an
> external zserver using the hostname and port parameters.  If the ping works
> it returns something like:
> 
> Z39.50 server at www.ga.gov.au on port 6668 is alive.
> 
>  and then exits the pipe.
> 
> Sometimes it can't zping because the zserver is down or the zping command is
> not allowed through a firewall so the pipe hangs for 224 seconds.  
> 
> There is an option of -t NN which sets how long to wait for a response from
> the zserver and I set this to 60 seconds (zping -t 60) but this doesn't seem
> to work when the pipe hangs.
> 
> I have tried to overcome this by using a combination of PERL while and sleep
> but I just can't seem to get it to work.  Here is a snippet of my code:
> 
> #################################################################
> #!/opt/csw/bin/perl
> ###################
> # sleep for 60 seconds or zping
> ###################
>   $hostname = shift;
>   $port = shift;
> 
>   $zping_cmd = '/public/data/asddgate/isite/current/bin/zping';
>   my ($alive);
> 
>   open (ZPING, "$zping_cmd -t 60 $hostname $port 2>&1 |")
>     or die "Cannot open zping: $!";
>   while (($results = <ZPING>) || (sleep(60) != 60) ) {
>     print "$results\n";
>     if ($results =~ m/is alive/) {
>       $alive = 1;
>       print "$results\n";
>       last;
>     }
>   }
>   print "closing ZPING and alive = $alive\n";
>   close ZPING;
> #################################################################
> 
> Am I doing this correctly or is there some other way to break this pipe after
> waiting for a set time and still continue with the code?  I tried alarm but
> it exits the script and I want to keep on processing.
> 
> Thanks.
> 
> 
>  John Hockaday
>  Geoscience Australia
>  GPO Box 378
>  Canberra ACT 2601
>  (02) 6249 9735
>  http://www.ga.gov.au/ 
>  john.hockaday\@ga.gov.au
> _______________________________________________
> Canberra-pm mailing list
> Canberra-pm at pm.org
> http://mail.pm.org/mailman/listinfo/canberra-pm


More information about the Canberra-pm mailing list