I can replicate that the first snippet doesn't seem to output anything. However, it seems that the signal does get triggered, it just has something to do with buffering, as the following two examples work. Are you really just doing print "Timeout reached"; in your original code, without the newline or anything else that would cause the output to get flushed? See also Suffering from Buffering.

$ perl -wMstrict -e 'alarm 2; $SIG{ALRM}=sub{ print "Timeout reached" }; while(){}' ^C $ perl -wMstrict -e 'alarm 2; $SIG{ALRM}=sub{ print "Timeout reached\n" }; while(){}' Timeout reached $ perl -wMstrict -e '$|++; alarm 2; $SIG{ALRM}=sub{ print "Timeout reached" }; while(){}' Timeout reached

Elsewhere in this thread you said:

My actual perl program is huge to post it here. It does not use threads but it execute many external commands.

Well, the trick is to reduce the actual code down to an SSCCE. Delete some code from your program, if the problem persists, that code can stay deleted, but if the problem goes away, then put that code back in. Repeat this over and over until your code is a 10 to 20 line program that demonstrates the issue. Not only will this help you in narrowing down the problem, it'll give us a way to reproduce the actual issue ourselves.

Other than the above buffering issue, I wouldn't exclude the possibility that one of the external programs is the culprit, or something else you're doing in your "huge" program. For example, perhaps something questionable is happening to STDOUT.


In reply to Re^2: Print inside SIGNALS by haukex
in thread Print inside SIGNALS by pedrete

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.