xiper has asked for the wisdom of the Perl Monks concerning the following question:
I pretty new to the world of pipe/fork/exec stuff, so please excuse any ignorance!$exit = &command( "/bin/snore", 60 );
Along with the quote:eval { local $SIG{ALRM} = sub { die "alarm clock restart" }; alarm 10; flock(FH, 2); # blocking write lock alarm 0; }; if ($@ and $@ !~ /alarm clock restart/) { die }
| If the operation being timed out is system() or qx(), this technique is liable to generate zombies. If this matters to you, you'll need to do your own fork() and exec(), and kill the errant child process. |
#! /usr/bin/perl use strict; my $exit = &command( "sleep 4; exit 99;", 1 ); if(! defined( $exit ) ) { print "error: command timed-out\n" } else { print "yay, it worked, exit: $exit\n" } sub command { my( $command, $timeout ) = @_; my $pid; eval { local $SIG{ALRM} = sub { die }; if( $pid = fork ) { alarm $timeout } else { exec( $command ) || die( "Couldn't exec $comma +nd" ) } waitpid( $pid, 0 ); alarm 0; }; if( $@ ) { return undef } else { return $? / 256 } }
my JAPH: print"Just another Perl hacker\n"; # ^ look, no space! pretty tricky hey?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: The 'ol shell timeout question
by Zaxo (Archbishop) on Jun 18, 2003 at 02:44 UTC | |
| |
|
Re: The 'ol shell timeout question
by hacker (Priest) on Jun 18, 2003 at 02:24 UTC |