Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Using SIG{ALRM} within while loop.

by bliako (Monsignor)
on Jul 24, 2019 at 13:33 UTC ( [id://11103300]=note: print w/replies, xml ) Need Help??


in reply to Using SIG{ALRM} within while loop.

and here is a much cleaner solution with IO::Select and a timeout:

#!/usr/bin/perl use strict; use warnings; use Time::Local; use Time::HiRes qw[ time ]; use POSIX ":sys_wait_h"; use IO::Select; # if command ended, do not keep it because that makes it zombie # this or reap it using waitpid below #$SIG{CHLD} = 'IGNORE'; my $timeout = 3; my $st=time; my $Command = qq(echo 'Hallo, sleeping for 12 seconds ...'; sleep 12); my $COMMANDFH; my $pid = open ($COMMANDFH, '-|', "$Command"); my $select = IO::Select->new(); $select->add($COMMANDFH); print "ran command with pid=$pid\n"; my $iters = 0; WH: while (++$iters) { last WH if waitpid $pid, WNOHANG; my @ready = $select->can_read($timeout); print "no output for the last $timeout seconds ...\n" unless scala +r @ready; foreach(@ready){ last WH if waitpid $pid, WNOHANG; print "command got: ".<$_>."\n"; } sleep 1; } print "command has ended after ".(time-$st)." seconds.\n";

bw, bliako

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11103300]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found