Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # -*- tab-width: 4 -*- ;; Emacs
- # vi: set tabstop=4 :: Vi/ViM
- ############################################################ GLOBALS
- # Global exit status variables
- SUCCESS=0
- FAILURE=1
- ############################################################ FUNCTIONS
- # timeout_watcher $nsecs
- #
- # No need to call directly. Used by eval_timeout in the following manner:
- # eval_timeout $nsecs $cmd ... | timeout_watcher $nsecs
- #
- timeout_watcher()
- {
- local timeout="$1" tPID tALIVE
- read tPID
- while :; do
- kill -INFO $tPID 2> /dev/null || break
- read -t "$timeout" tALIVE
- if [ ! "$tALIVE" ]; then
- # The SIGINFO trap didn't respond in the given timeout...
- # ... assume the sub-shell has hung and kill it.
- kill -9 $tPID
- break
- fi
- sleep $timeout
- done
- }
- # eval_timeout $nsecs $cmd ...
- #
- eval_timeout()
- {
- local timeout="$1"
- [ "$timeout" ] || return $FAILURE
- shift
- cat <<-EOF | sh -T | timeout_watcher $timeout
- trap 'echo still alive' INFO
- echo \$\$
- eval "$@" 2>&1
- EOF
- }
- ############################################################ MAIN
- eval_timeout "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement