Hi,

I would like to achieve the following:

Run a command and capture its output. While waiting for output, run a subroutine every 10 seconds. So, I do not want to escape from the while loop, like in most other examples.

In this example, the timer is set off, but it ends the program. I tried positioning the eval block on various places (e.g. outside the while loop), but to no avail. Perhaps it cannot be done and I need to dive into working with threads.

The sleep 120 in this code is only to simulate no response from command.

#!/usr/bin/perl use strict; use warnings; use Time::Local; use Time::HiRes qw[ time ]; use POSIX ":sys_wait_h"; my $ReturnValue = 0; sub MainSub() { my $Sub = "MainSub"; my $SubReturnValue = 0; my $CommandLine; my $Command = qq(echo 'Hallo, sleeping for 120 seconds ...'; sleep 120 + 2>&1); open (COMMAND, '-|', "$Command"); while (<COMMAND>) { eval { local $SIG{ALRM} = sub { die("TimerTriggerStatus\n"); }; alarm(10); printf "%s : %s\n", scalar localtime(), "$Sub - timer set to 1 +0 seconds"; chomp; $CommandLine = $_; printf "%s : %s\n", scalar localtime(), "$Sub - CommandLine re +ceived:\n$CommandLine"; }; if ( $@ eq "TimerTriggerStatus\n" ) { printf "%s : %s\n", scalar localtime(), "$Sub - timer triggere +d"; &TimerHandler; }; } close (COMMAND); return ($SubReturnValue); } # end of sub MainSub sub TimerHandler() { my $Sub = "TimerHandler"; my $SubReturnValue = 0; printf "%s : %s\n", scalar localtime(), "$Sub - timer expired"; alarm(10); printf "%s : %s\n", scalar localtime(), "$Sub - timer set to 10 second +s"; return ($SubReturnValue); } # end of sub TimerHandler sub TimerSub() { my $Sub = "TimerSub"; my $SubReturnValue = 0; printf "%s : %s\n", scalar localtime(), "$Sub - run after timer interr +upt"; alarm(10); printf "%s : %s\n", scalar localtime(), "$Sub - timer set to 10 second +s"; return ($SubReturnValue); } # end of sub TimerSub #### MAIN #### printf "%s : %s\n", scalar localtime(), "BEGIN - MAIN"; ($ReturnValue) = &MainSub; printf "%s : %s\n", scalar localtime(), "END - MAIN";

Output when run:

# time ./TestSigalrm.pl Wed Jul 24 08:38:20 2019 : BEGIN - MAIN Wed Jul 24 08:38:20 2019 : MainSub - timer set to 10 seconds Wed Jul 24 08:38:20 2019 : MainSub - CommandLine received: Hallo, sleeping for 120 seconds ... Alarm clock real 0m10.018s user 0m0.014s sys 0m0.002s

In reply to Using SIG{ALRM} within while loop. by evroom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.