Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6916861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:39:27+00:00 2026-05-27T09:39:27+00:00

I have a perl script that runs a series of batch scripts for regression

  • 0

I have a perl script that runs a series of batch scripts for regression testing. I want to implement a timeout on the batch scripts. I currently have the following code.

    my $pid = open CMD, "$cmd 2>&1 |";
    eval {
               # setup the alarm
               local $SIG{ALRM} = sub { die "alarm\n" };
               # alarm on the timeout
               alarm $MAX_TIMEOUT;
               log_output("setting alarm to $MAX_TIMEOUT\n");

               # run our exe
               while( <CMD> ) {
                       $$out_ref .= $_;
               }
               $timeRemaining = alarm 0;
            };
            if ($@) {
                    #catch the alarm, kill the executable
            }

The problem is that no matter what I set the max timeout to, the alarm is never tripped. I’ve tried using Perl::Unsafe::Signals but that did not help.

Is this the best way to execute the batch scripts if I want to be able to capture their output? Is there another way that would do the same thing that would allow me to use alarms, or is there another method besides alarms to timeout the program?

I have built a test script to confirm that alarm works on with my perl and windows version, but it does not work when I run a command like this.

I’m running this with activeperl 5.10.1 on windows 7 x64.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:39:27+00:00Added an answer on May 27, 2026 at 9:39 am

    It’s hard to tell when alarm will work, when a system call will and won’t get interrupted by a SIGALRM, how the same code might behave differently on different operating systems, etc.

    If your job times out, you want to kill the subprocess you have started. This is a good use case for the poor man’s alarm:

    my $pid = open CMD, "$cmd 2>&1 |";
    my $time = $MAX_TIMEOUT;
    
    my $poor_mans_alarm = "sleep 1,kill(0,$pid)||exit for 1..$time;kill -9,$pid";
    if (fork() == 0) {
        exec($^X, "-e", $poor_mans_alarm);
        die "Poor man's alarm failed to start";  # shouldn't get here
    }
    # on Windows, instead of  fork+exec, you can say
    #    system 1, qq[$^X -e "$poor_mans_alarm"]
    
    
    ...
    

    The poor man’s alarm runs in a separate process. Every second, it checks whether the process with identifier $pid is still alive. If the process isn’t alive, the alarm process exits. If the process is still alive after $time seconds, it sends a kill signal to the process (I used 9 to make it untrappable and -9 to take out the whole subprocess tree, your needs may vary).

    (The exec actually may not be necessary. I use it because I also use this idiom to monitor processes that might outlive the Perl script that launched them. Since that wouldn’t be the case with this problem, you could skip the exec call and say

    if (fork() == 0) {
        for (1..$time) { sleep 1; kill(0,$pid) || exit }
        kill -9, $pid;
        exit;
    }
    

    instead.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have a perl script that runs forever on my server, checking a
I currently have a Perl script that runs an external command on the system,
I have written a Perl script that runs as a daily crontab job that
I have a quite simple perl script, that in one function does the following:
Started to learn some shell scripting. I have a perl script that runs on
I have a perl script that runs as a daemon looping all the time.
I have a Perl script that runs a different utility (called Radmind , for
I have a perl script that I need to tweak. The script runs and
I have a Perl script that I'm attempting to set up using Perl Threads
I have a Perl script that sets up variables near the top for directories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.