Your script dies because you call alarm() where there is no handler in scope. The code as it stands is rather hard to follow for me because you have used modules you don't then refer to, you have a sub TimerSub which you never call, you have a load of printfs which almost all do the same thing, the subs aren't indented and you are using prototypes with no comments as to why.

Removing/replacing all of this we get:

#!/usr/bin/perl use strict; use warnings; sub TimerHandler() { debug ("In the handler"); } debug ('BEGIN - MAIN'); my $Command = qq(echo 'Hallo, sleeping for 120 seconds ...'; sleep 120 + 2>&1); open (COMMAND, '-|', $Command); $SIG{ALRM} = sub { die("TimerTriggerStatus\n"); }; while (<COMMAND>) { eval { alarm(10); debug ("timer set to 10 seconds"); chomp; debug ("CommandLine received:\n$_"); }; if ( $@ eq "TimerTriggerStatus\n" ) { debug ("timer triggered"); TimerHandler (); }; } close (COMMAND); debug ('END - MAIN'); sub debug { printf "%s : %s - %s\n", scalar localtime(), (caller(1))[3], shift +; }

which still doesn't do what you want but is at least easier to trace through and hopefully you can see the problem which is that your eval does not cover the blocking read and therefore the script dies but at least it does so with your handler.

Perhaps it cannot be done and I need to dive into working with threads.

It's probably feasible without threads or forks or a framework around them but it will be hackish. Why not use this as an excellent opportunity to try a threaded or multiprocess approach with a small, manageable task? It probably won't be as tough as you think.

Good luck.


In reply to Re: Using SIG{ALRM} within while loop. by hippo
in thread 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.