Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i have written a perl script for running test. test run for 3 days or more. i have to modify script such that i set a alarm for 3 days or some specified hours form command line window.
after 3 days the alarm should expire and i will kill test when it expires.
how is it possible to do in perl.

Replies are listed 'Best First'.
Re: how to set alarm in perl
by shmem (Chancellor) on Sep 11, 2006 at 07:19 UTC
    See alarm.

    $SIG{ALRM} = sub { warn "3 days over!\n"; exit }; alarm(86400 * 3);

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      now i m confused
      the above code will kill tests after there days. and it prints messages " 3 days over" and exits
      i can also write a code to print final report inside the sub function
      if test get over before 3 days then where should i place code to finally create a report.
      i mean where to place code to check if test over before 3. should it come after above code or before above code.
        $SIG{ALRM} = sub { report(); exit }; alarm(86400 * 3); # your test code here ... # at the end, print report report(); sub report { # your reporting code }

        At the end of your test script you invoke report(). If the script happens to run for three days, the alarm handler will invoke report() and terminate the script.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: how to set alarm in perl
by ysth (Canon) on Sep 11, 2006 at 07:54 UTC
    It's not clear to me whether you want to set an alarm in your long-running perl script to end it after 3 days, or to run another program that kills your long-running script after it's run 3 days. Can you clarify?
      this is what i m need to do.i had script which submit tests to unix LSF. its expected that tests will end within 3 days.
      but in worse loading it may happen tests dont complete in 3 days, in that case i will set a alarm for 3 days and will kill tests after 3 days.
      if tests end with in three days i will create final report log.
      i m checking status of servers to find if all tests are done or not using bsub commands.
      in short i want
      ------ see if alarm is not expired ------ see serve load , if server load ==0 , all tests done , create final report. ------ if alam expired kill all tests. hope this explains what i need.
        No need for alarm, then. Just write what you want to have.

        Place your code which checks the server load into a loop which terminates when the time passed hits the timeout. Exit the loop if conditions for reporting are met. After the loop, do the reporting.

        Write your script. If you have questions around it, post it, and ask. See How do I post a question effectively?.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}