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 845295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:25:34+00:00 2026-05-15T06:25:34+00:00

I created a script in perl to run programs with a timeout. If the

  • 0

I created a script in perl to run programs with a timeout. If the program being executed takes longer then the timeout than the script kills this program and returns the message “TIMEOUT”.

The script worked quite well until I decided to redirect the output of the executed program.

When the stdout and stderr are being redirected, the program executed by the script is not being killed because it has a pid different than the one I got from fork.

It seems perl executes a shell that executes my program in the case of redirection.

I would like to have the output redirection but still be able to kill the program in the case of a timeout.

Any ideas on how I could do that?

A simplified code of my script is:

#!/usr/bin/perl

use strict;
use warnings;
use POSIX ":sys_wait_h";

my $timeout = 5;
my $cmd = "very_long_program 1>&2 > out.txt";

my $pid = fork();
if( $pid == 0 )
{
   exec($cmd) or print STDERR "Couldn't exec '$cmd': $!";
   exit(2);
}
my $time = 0;
my $kid = waitpid($pid, WNOHANG);
while ( $kid == 0 )
{
   sleep(1);
   $time ++;
   $kid = waitpid($pid, WNOHANG);
   print "Waited $time sec, result $kid\n";
   if ($timeout > 0 && $time > $timeout)
   {
      print "TIMEOUT!\n";
      #Kill process
      kill 9, $pid;
      exit(3);
   }
}

if ( $kid == -1)
{
   print "Process did not exist\n";
   exit(4);
}
print "Process exited with return code $?\n";
exit($?);

Thanks for any help.

  • 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-15T06:25:35+00:00Added an answer on May 15, 2026 at 6:25 am

    Try changing $cmd from

    my $cmd = "very_long_program 1>&2 > out.txt";
    

    to

    my $cmd = "exec very_long_program 1>&2 > out.txt";
    

    The exec will tell the shell that gets spawned by perl to replace itself with very_long_program, rather than running very_long_program as a child.

    (The reason perl spawns a shell in this case is because $cmd contains the redirect characters – and perl doesn’t know how to handle them itself. An alternative way of solving the problem is to do the redirection in perl itself after the fork() but prior to calling exec() – but that’s slightly trickier, so try the exec workaround first!)

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

Sidebar

Related Questions

My name is abbi My first perl script run on linux machine This script
Has anyone run perl script given at http://oreilly.com/pub/h/974#code ? This is a famous one,
I'm trying to run a perl script through C# code. The perl script takes
I am trying to run this script mail.pl #!/usr/bin/perl print Content-type: text/html \n\n; #HTTP
I have a Perl script that takes user input and creates another script that
I have created a script to run a test script on a batch of
I'm looking to create a perl script that I will run on a JavaScript
I want to run my perl script< ftp_files_copy.pl> on windows through batch file<Run_perl_script.bat> at
My Perl script needs to run multiple threads simultaneously... use threads ('yield', 'exit' =>
I have created an executable (.exe) of a perl script on a 64-bit linux

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.