diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2010-10-11 14:49:26 -0700 |
---|---|---|
committer | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2010-10-11 15:19:40 -0700 |
commit | 011c1d1c0766c65517ebd495465c99e86edb63ec (patch) | |
tree | 30d8f6a13235af90897c3223554871ef52225462 /examples/scripts | |
parent | 40cfaccf7b178b6239b5cd0013ef80b7ff8e503e (diff) | |
download | vyatta-bash-011c1d1c0766c65517ebd495465c99e86edb63ec.tar.gz vyatta-bash-011c1d1c0766c65517ebd495465c99e86edb63ec.zip |
Update to bash-4.1
Diffstat (limited to 'examples/scripts')
-rw-r--r-- | examples/scripts/cat.sh | 2 | ||||
-rwxr-xr-x | examples/scripts/timeout2 | 29 | ||||
-rw-r--r-- | examples/scripts/timeout3 | 91 |
3 files changed, 121 insertions, 1 deletions
diff --git a/examples/scripts/cat.sh b/examples/scripts/cat.sh index 78106b2..3e65b3f 100644 --- a/examples/scripts/cat.sh +++ b/examples/scripts/cat.sh @@ -1,7 +1,7 @@ shcat() { while read -r ; do - echo "$REPLY" + printf "%s\n" "$REPLY" done } 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 diff --git a/examples/scripts/timeout3 b/examples/scripts/timeout3 new file mode 100644 index 0000000..5c19d2e --- /dev/null +++ b/examples/scripts/timeout3 @@ -0,0 +1,91 @@ +#!/bin/bash +# +# The Bash shell script executes a command with a time-out. +# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal +# is blocked, then the subsequent SIGKILL (9) terminates it. +# +# Based on the Bash documentation example. + +# Hello Chet, +# please find attached a "little easier" :-) to comprehend +# time-out example. If you find it suitable, feel free to include +# anywhere: the very same logic as in the original examples/scripts, a +# little more transparent implementation to my taste. +# +# Dmitry V Golovashkin <Dmitry.Golovashkin@sas.com> + +scriptName="${0##*/}" + +declare -i DEFAULT_TIMEOUT=9 +declare -i DEFAULT_INTERVAL=1 +declare -i DEFAULT_DELAY=1 + +# Timeout. +declare -i timeout=DEFAULT_TIMEOUT +# Interval between checks if the process is still alive. +declare -i interval=DEFAULT_INTERVAL +# Delay between posting the SIGTERM signal and destroying the process by SIGKILL. +declare -i delay=DEFAULT_DELAY + +function printUsage() { + cat <<EOF + +Synopsis + $scriptName [-t timeout] [-i interval] [-d delay] command + Execute a command with a time-out. + Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM + signal is blocked, then the subsequent SIGKILL (9) terminates it. + + -t timeout + Number of seconds to wait for command completion. + Default value: $DEFAULT_TIMEOUT seconds. + + -i interval + Interval between checks if the process is still alive. + Positive integer, default value: $DEFAULT_INTERVAL seconds. + + -d delay + Delay between posting the SIGTERM signal and destroying the + process by SIGKILL. Default value: $DEFAULT_DELAY seconds. + +As of today, Bash does not support floating point arithmetic (sleep does), +therefore all delay/time values must be integers. +EOF +} + +# Options. +while getopts ":t:i:d:" option; do + case "$option" in + t) timeout=$OPTARG ;; + i) interval=$OPTARG ;; + d) delay=$OPTARG ;; + *) printUsage; exit 1 ;; + esac +done +shift $((OPTIND - 1)) + +# $# should be at least 1 (the command to execute), however it may be strictly +# greater than 1 if the command itself has options. +if (($# == 0 || interval <= 0)); then + printUsage + exit 1 +fi + +# kill -0 pid Exit code indicates if a signal may be sent to $pid process. +( + ((t = timeout)) + + while ((t > 0)); do + sleep $interval + kill -0 $$ || exit 0 + ((t -= interval)) + done + + # Be nice, post SIGTERM first. + # The 'exit 0' below will be executed if any preceeding command fails. + kill -s SIGTERM $$ && kill -0 $$ || exit 0 + sleep $delay + kill -s SIGKILL $$ +) 2> /dev/null & + +exec "$@" |