aboutsummaryrefslogtreecommitdiffstats
path: root/examples/scripts/timeout3
diff options
context:
space:
mode:
Diffstat (limited to 'examples/scripts/timeout3')
-rw-r--r--examples/scripts/timeout383
1 files changed, 42 insertions, 41 deletions
diff --git a/examples/scripts/timeout3 b/examples/scripts/timeout3
index 5c19d2e..de650be 100644
--- a/examples/scripts/timeout3
+++ b/examples/scripts/timeout3
@@ -1,56 +1,57 @@
#!/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
+# The Bash script executes a command with a time-out.
+# Based on the Bash documentation example.
+#
+# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal
# is blocked, then the subsequent SIGKILL (9) terminates it.
+# Dmitry V Golovashkin (E-mail: dvg@ieee.org)
#
-# Based on the Bash documentation example.
+script_name="${0##*/}"
-# 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>
+# Default values.
+readonly param_timeout=5
+readonly param_interval=1
+readonly param_delay=1
-scriptName="${0##*/}"
+declare -i timeout=param_timeout
+declare -i interval=param_interval
+declare -i delay=param_delay
-declare -i DEFAULT_TIMEOUT=9
-declare -i DEFAULT_INTERVAL=1
-declare -i DEFAULT_DELAY=1
+blue="$(tput setaf 4)"
+bold_red="$(tput bold; tput setaf 1)"
+off="$(tput sgr0)"
-# 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 print_usage() {
+cat <<EOF
-function printUsage() {
- cat <<EOF
+Synopsis: $script_name [-t timeout] [-i interval] [-d delay] command
-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.
+Executes the 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.
+$blue-t timeout$off
+ Number of seconds to wait for command completion.
+ Default value: $param_timeout seconds. In some practical situations
+ this value ${bold_red}must$off be increased (for instance -t 180) to allow
+ the command to complete.
- -i interval
- Interval between checks if the process is still alive.
- Positive integer, default value: $DEFAULT_INTERVAL seconds.
+$blue-i interval$off
+ Interval between checks if the process is still alive.
+ Positive integer, default value: $param_interval seconds.
+ Default value is OK for most situations.
- -d delay
- Delay between posting the SIGTERM signal and destroying the
- process by SIGKILL. Default value: $DEFAULT_DELAY seconds.
+$blue-d delay$off
+ Delay between posting the SIGTERM signal and destroying the process by
+ SIGKILL. Default value: $param_delay seconds.
+ Default value is OK for most situations.
As of today, Bash does not support floating point arithmetic (sleep does),
-therefore all delay/time values must be integers.
+therefore all time values must be integers.
+Dmitry Golovashkin (E-mail: dvg@ieee.org)
EOF
+exit 1 # No useful work was done.
}
# Options.
@@ -59,7 +60,7 @@ while getopts ":t:i:d:" option; do
t) timeout=$OPTARG ;;
i) interval=$OPTARG ;;
d) delay=$OPTARG ;;
- *) printUsage; exit 1 ;;
+ *) print_usage ;;
esac
done
shift $((OPTIND - 1))
@@ -67,11 +68,10 @@ 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
+ print_usage
fi
-# kill -0 pid Exit code indicates if a signal may be sent to $pid process.
+# kill -0 pid Exit code indicates if a signal may be sent to "pid" process.
(
((t = timeout))
@@ -89,3 +89,4 @@ fi
) 2> /dev/null &
exec "$@"
+