ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/DistributedModelCalculations/Utilities/TimeOut
Revision: 1.1
Committed: Thu May 17 12:01:33 2012 UTC (12 years, 11 months ago) by buchmann
Branch: MAIN
Log Message:
added timeout script

File Contents

# User Rev Content
1 buchmann 1.1 #!/bin/bash
2     #
3     # This script is based on a script found here:
4     # http://www.bashcookbook.com/bashinfo/source/bash-4.0/examples/scripts/timeout3
5     #
6     # The original description is found below
7     # ##############################################################
8     #
9     # The Bash shell script executes a command with a time-out.
10     # Upon time-out expiration SIGTERM (15) is sent to the process. If the signal
11     # is blocked, then the subsequent SIGKILL (9) terminates it.
12     #
13     # Based on the Bash documentation example.
14    
15     # Hello Chet,
16     # please find attached a "little easier" :-) to comprehend
17     # time-out example. If you find it suitable, feel free to include
18     # anywhere: the very same logic as in the original examples/scripts, a
19     # little more transparent implementation to my taste.
20     #
21     # Dmitry V Golovashkin <Dmitry.Golovashkin@sas.com>
22    
23     scriptName="${0##*/}"
24    
25     declare -i DEFAULT_TIMEOUT=9
26     declare -i DEFAULT_INTERVAL=1
27     declare -i DEFAULT_DELAY=1
28    
29     # Timeout.
30     declare -i timeout=DEFAULT_TIMEOUT
31     # Interval between checks if the process is still alive.
32     declare -i interval=DEFAULT_INTERVAL
33     # Delay between posting the SIGTERM signal and destroying the process by SIGKILL.
34     declare -i delay=DEFAULT_DELAY
35    
36     function printUsage() {
37     cat <<EOF
38    
39     Synopsis
40     $scriptName [-t timeout] [-i interval] [-d delay] command
41     Execute a command with a time-out.
42     Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM
43     signal is blocked, then the subsequent SIGKILL (9) terminates it.
44    
45     -t timeout
46     Number of seconds to wait for command completion.
47     Default value: $DEFAULT_TIMEOUT seconds.
48    
49     -i interval
50     Interval between checks if the process is still alive.
51     Positive integer, default value: $DEFAULT_INTERVAL seconds.
52    
53     -d delay
54     Delay between posting the SIGTERM signal and destroying the
55     process by SIGKILL. Default value: $DEFAULT_DELAY seconds.
56    
57     As of today, Bash does not support floating point arithmetic (sleep does),
58     therefore all delay/time values must be integers.
59     EOF
60     }
61    
62     # Options.
63     while getopts ":t:i:d:" option; do
64     case "$option" in
65     t) timeout=$OPTARG ;;
66     i) interval=$OPTARG ;;
67     d) delay=$OPTARG ;;
68     *) printUsage; exit 1 ;;
69     esac
70     done
71     shift $((OPTIND - 1))
72    
73     # $# should be at least 1 (the command to execute), however it may be strictly
74     # greater than 1 if the command itself has options.
75     if (($# == 0 || interval <= 0)); then
76     printUsage
77     exit 1
78     fi
79     export HAVEKILLED=0
80     # kill -0 pid Exit code indicates if a signal may be sent to $pid process.
81     (
82     ((t = timeout))
83    
84     while ((t > 0)); do
85     sleep $interval
86     kill -0 $$ || exit 0
87     ((t -= interval))
88     done
89    
90     # Be nice, post SIGTERM first.
91     # The 'exit 0' below will be executed if any preceeding command fails.
92     echo "Sorry have to kill the process as it took longer than $timeout seconds (checked after $interval)"
93     kill -s SIGTERM $$ && kill -0 $$ || exit 0
94     export HAVEKILLED=1
95     sleep $delay
96     kill -s SIGKILL $$
97     ) 2> /dev/null &
98    
99     exec "$@"