From 772f20abb0a3a0979c440114bf3a1cff5b3cef03 Mon Sep 17 00:00:00 2001 From: cvpcs Date: Wed, 2 Jun 2010 11:02:31 -0500 Subject: initial import of bash 4.1 --- examples/scripts/timeout2 | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 examples/scripts/timeout2 (limited to 'examples/scripts/timeout2') diff --git a/examples/scripts/timeout2 b/examples/scripts/timeout2 new file mode 100755 index 0000000..2c6fb77 --- /dev/null +++ b/examples/scripts/timeout2 @@ -0,0 +1,29 @@ +#!/bin/sh + +# Author: P@draigBrady.com +# V1.0 : Nov 3 2006 +# +# Execute a command with a timeout. +# If the timeout occurs the exit status is 128 +# +# Note there is an asynchronous equivalent of this +# script packaged with bash (under /usr/share/doc/ in my distro), +# which I only noticed after writing this. + +if [ "$#" -lt "2" ]; then + echo "Usage: `basename $0` timeout_in_seconds command" >&2 + echo "Example: `basename $0` 2 sleep 3 || echo timeout" >&2 + exit 1 +fi + +cleanup() +{ + kill %1 2>/dev/null #kill sleep $timeout if running + kill %2 2>/dev/null && exit 128 #kill monitored job if running +} + +set -m #enable job control +trap "cleanup" 17 #cleanup after timeout or command +timeout=$1 && shift #first param is timeout in seconds +sleep $timeout& #start the timeout +"$@" #start the job -- cgit v1.1