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 |
– |
# |
3 |
|
# The Bash shell script executes a command with a time-out. |
4 |
|
# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal |
5 |
|
# is blocked, then the subsequent SIGKILL (9) terminates it. |
16 |
|
|
17 |
|
scriptName="${0##*/}" |
18 |
|
|
19 |
< |
declare -i DEFAULT_TIMEOUT=9 |
19 |
> |
declare -i DEFAULT_TIMEOUT=300 |
20 |
|
declare -i DEFAULT_INTERVAL=1 |
21 |
|
declare -i DEFAULT_DELAY=1 |
22 |
|
|
54 |
|
} |
55 |
|
|
56 |
|
# 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)) |
57 |
|
|
58 |
|
# $# should be at least 1 (the command to execute), however it may be strictly |
59 |
|
# greater than 1 if the command itself has options. |
74 |
|
|
75 |
|
# Be nice, post SIGTERM first. |
76 |
|
# The 'exit 0' below will be executed if any preceeding command fails. |
77 |
< |
echo "Sorry have to kill the process as it took longer than $timeout seconds (checked after $interval)" |
77 |
> |
echo -e "\E[31;47m Sorry have to kill the process as it took longer than $timeout seconds (checked after $interval)\E[0m \n " |
78 |
|
kill -s SIGTERM $$ && kill -0 $$ || exit 0 |
79 |
|
export HAVEKILLED=1 |
80 |
|
sleep $delay |
81 |
|
kill -s SIGKILL $$ |
82 |
|
) 2> /dev/null & |
83 |
|
|
84 |
< |
exec "$@" |
84 |
> |
eval $@ |