#!/usr/bin/ruby -w # $Id: impatient,v 1.4 2006/11/24 14:34:16 mark Exp $ if ARGV.length < 3 print < Returns 's return code if it completed in , or otherwise. EOF exit end timeoutSeconds, timeoutReturnValue, *command = ARGV childpid = nil # Add a handler for SIGCHLD: trap("CLD") { endedpid = Process.wait if endedpid == childpid exit $? else # Otherwise some other child process exited, do nothing... end } # We fork, and run command in the child. After sleeping for timeout # seconds in the parent, we kill the child. The parent will have # already exited via the handler for SIGCHLD if the command has # completed... childpid = fork if childpid # In the parent.. begin sleep timeoutSeconds.to_i rescue print "Couldn't sleep for #{timeoutSeconds} seconds...\n" exit end Process.kill( "SIGTERM", -childpid ) exit timeoutReturnValue.to_i else # In the child... # We want to be able kill all the child processes as well, so set # the process group to this PID: Process.setpgrp exec( *command ) end