Versions: Perl 5.10, Apache 2.x, CentOS 6

(modified since originally posted, so that process A kicks off the scripts, rather than using an intermediary script to do it)

I have a Perl script (process A) which kicks off several other Perl scripts. Process A is kicked off via http.

I am trying to get process A to wait for x seconds after having executed the other scripts, which may have finished, or they may not have done; I don’t really care – I just need to wait x seconds in process A, and then continue.

I’ve tried several ways of doing this, but can’t seem to get it working as I want. Partly this is probably because I don’t really understand signals etc, etc properly. I used to have a way of making it work in Apache 1.3, but Apache 2.x has changed how it handles such things I think.

So, code which I can get to work as a single process (purloined from StackOverflow), as a test, is below:

#!/usr/bin/perl use strict; use My_Functions; &print_myheader(); my $br=(-t)?"\n":'<br />'; use POSIX qw(:signal_h); my $sigset_new = POSIX::SigSet->new(); my $sigset_old = POSIX::SigSet->new(); sigprocmask(SIG_BLOCK, $sigset_new, $sigset_old); if ($sigset_old->ismember(SIGALRM)) { print "SIGALRM is being blocked!$br"; $sigset_new->addset(SIGALRM); sigprocmask(SIG_UNBLOCK, $sigset_new); } else { print "SIGALRM NOT being blocked$br"; } $SIG{ALRM} = sub {print scalar(localtime()), " ALARM, leaving$br"; sig +procmask(SIG_BLOCK, $sigset_new, $sigset_old); exit; }; alarm(5); print scalar(localtime()), " Starting sleep...$br"; sleep (10); print scalar(localtime()), " Exiting normally...$br";

This does as expected, and exits after five seconds.

However, I’m not sure if it’s because I am not implementing the code properly, but I just can’t seem to get it working, and I can get no http output until each of the kicked-off scripts has finished.

The relevant snippet of my code from process A looks like this:

[…. code …] my $sigset_new = POSIX::SigSet->new(); my $sigset_old = POSIX::SigSet->new(); sigprocmask(SIG_BLOCK, $sigset_new, $sigset_old); if ($sigset_old->ismember(SIGALRM)) { $sigset_new->addset(SIGALRM); sigprocmask(SIG_UNBLOCK, $sigset_new); } else { # do nothing } eval { $SIG{ALRM}=sub{ sigprocmask(SIG_BLOCK, $sigset_new, $sigset_old); exit; }; alarm ($timer); for (@mylist) { my $cmd= "perl process_$_.pl args"; # tried with trailing & too system($cmd); } alarm (0); }; [ … more code …]

Any assistance would be greatly appreciated! Thanks.


In reply to Perl / Apache 2 / Alarms by DanielSpaniel

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.