summaryrefslogtreecommitdiff
path: root/examples/scripts/timeout2
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2010-10-11 14:49:26 -0700
committerStephen Hemminger <stephen.hemminger@vyatta.com>2010-10-11 15:19:40 -0700
commit011c1d1c0766c65517ebd495465c99e86edb63ec (patch)
tree30d8f6a13235af90897c3223554871ef52225462 /examples/scripts/timeout2
parent40cfaccf7b178b6239b5cd0013ef80b7ff8e503e (diff)
downloadvyatta-bash-011c1d1c0766c65517ebd495465c99e86edb63ec.tar.gz
vyatta-bash-011c1d1c0766c65517ebd495465c99e86edb63ec.zip
Update to bash-4.1
Diffstat (limited to 'examples/scripts/timeout2')
-rwxr-xr-xexamples/scripts/timeout229
1 files changed, 29 insertions, 0 deletions
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