Date: Thu, 27 Jan 2011 17:38:48 +0000 From: Devin Teske <dteske@vicor.com> To: Aryeh Friedman <aryeh.friedman@gmail.com> Cc: Devin Teske <dteske@vicor.com>, FreeBSD Mailing List <freebsd-questions@freebsd.org> Subject: Re: OT: How to set a timeout for a process Message-ID: <1296149928.20060.31.camel@dt.vicor.com> In-Reply-To: <AANLkTi=NJA4by3Pnv7f=KOh-R%2BSTq_aLiuS9K4FFO0_7@mail.gmail.com> References: <AANLkTi=NJA4by3Pnv7f=KOh-R%2BSTq_aLiuS9K4FFO0_7@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 2011-01-27 at 03:37 -0500, Aryeh Friedman wrote: > I have a script that may or not hang (the reasons why it hangs are > unimportant here) and need to call it from an other script and need to > say if it hangs to give up after X seconds and just continue the > script (no harm done if it fails) #!/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 "$@" > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1296149928.20060.31.camel>