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

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.

Replies are listed 'Best First'.
Re^2: Using SIG{ALRM} within while loop.
by evroom (Novice) on Jul 24, 2019 at 11:19 UTC

    Yes, you are right about unused subs. In my attempt post readable code I forgot the part where TimerHandler() calls TimerSub().

    In the meantime I started to tryout threads and it indeed is not too hard. Thanks for pointing out that is would be 'an excellent opportunity'