From owner-freebsd-questions@FreeBSD.ORG Thu Jan 27 17:38:44 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 82C871065670 for ; Thu, 27 Jan 2011 17:38:44 +0000 (UTC) (envelope-from dteske@vicor.com) Received: from postoffice.vicor.com (postoffice.vicor.com [69.26.56.53]) by mx1.freebsd.org (Postfix) with ESMTP id 6B1AE8FC21 for ; Thu, 27 Jan 2011 17:38:44 +0000 (UTC) Received: from [192.82.228.132] (port=50289) by postoffice.vicor.com with esmtpsa (SSLv3:AES256-SHA:256) (Exim 4.71) (envelope-from ) id 1PiVnt-00084D-LC; Thu, 27 Jan 2011 09:38:43 -0800 From: Devin Teske To: Aryeh Friedman In-Reply-To: References: Organization: VICOR, Inc. Date: Thu, 27 Jan 2011 17:38:48 +0000 Message-ID: <1296149928.20060.31.camel@dt.vicor.com> Mime-Version: 1.0 X-Mailer: Evolution 2.32.1 X-Scan-Signature: 72e9d3c6856f0c2b84fc3ccec67b7fdd X-Scan-Host: postoffice.vicor.com Content-Type: text/plain; charset="cp1252" Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: Devin Teske , FreeBSD Mailing List Subject: Re: OT: How to set a timeout for a process X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Jan 2011 17:38:44 -0000 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"