Haioken has asked for the wisdom of the Perl Monks concerning the following question:
While this operates correctly in allowing the perl program to continue after the timeout, in testing it's not terminating the spawned process. For example, if I call 'sleep 250' with a timeout of 10, the function will return after 10 seconds with "Command Timeout",1, however PS shows that the process 'sleep 250' is still running. Caveat: Note that because I'm looking for the returned data and exit code of the child process, exec_safe cannot return until it's finished or timed out. From a brief googlin', it doesn't appear that fork will allow me to get all the data I need. Any ideas?sub exec_safe { my @cmd; # Return variable eval { local $SIG{ALRM} = sub { die "Timeout\n" }; alarm $_[1]; @cmd = `nice -n $_[2] $_[0]`; alarm 0; }; if($@) { # If command fails, return non-zero and msg return ("Command timeout",1); } else { chomp @cmd; push(@cmd,$? >> 8); return @cmd; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Execute shell command, grab it's return, terminate on timeout
by wind (Priest) on Mar 21, 2014 at 00:52 UTC | |
by Haioken (Novice) on Mar 21, 2014 at 03:08 UTC |