MorgothSauron has asked for the wisdom of the Perl Monks concerning the following question:
During the test period, I noticed something strange. When using this code, my whole script is 'respawned' instead of the single command I want to check for timeout. Maybe the code is wrong. All I need is to run a single command and wait for the result withing a given timeout. If the command takes too long I want to kill it. The script is running on AIX 4.3.3 & 5.2. Perl version is 5.8. Any suggestions ? Thank you very much.$cmd="myCommand"; # Timeout evaluation: eval { # Set action to be done when alarm occurs: local $SIG{ALRM} = sub { die "ALARM_COMMAND_TIMEOUT" }; # Schedule alarm in $CMD_TIMEOUT seconds: alarm($CMD_TIMEOUT); ### execute the command that can block $childPID = open(PIPE, "-|"); ### Unable to create child process unless(defined $childPID) { close(PIPE); die "ERROR_FORK_PROCESS: $!\n"; } ### Child Process Created ... execute command unless($childPID) { exec($cmd) or die "ERROR_OPEN_PIPE : $!\n"; ### Unable to create child process } wait; alarm(0); close(PIPE); }; alarm(0);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Timeout with SIG{ALRM} not working as expected
by Random_Walk (Prior) on Jan 19, 2005 at 12:04 UTC | |
|
Re: Timeout with SIG{ALRM} not working as expected
by Anonymous Monk on Jan 19, 2005 at 11:37 UTC | |
by MorgothSauron (Initiate) on Jan 19, 2005 at 11:53 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |