From owner-freebsd-questions@FreeBSD.ORG Fri Jan 23 00:35:02 2009 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 8DE78106566B for ; Fri, 23 Jan 2009 00:35:02 +0000 (UTC) (envelope-from mkhitrov@gmail.com) Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.152]) by mx1.freebsd.org (Postfix) with ESMTP id 15B898FC0C for ; Fri, 23 Jan 2009 00:35:01 +0000 (UTC) (envelope-from mkhitrov@gmail.com) Received: by fg-out-1718.google.com with SMTP id l26so2849648fgb.35 for ; Thu, 22 Jan 2009 16:35:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=OsXjUUeYA4kvd1m6rlRauPehXzL90FQZBkrbg/wMZes=; b=b91Dw3HekroLK0YK1Fy6VHsJjJyLy8kQ8pG5hFwhXhQ73GxQZhsxCkyG1lutVzWFNM 7FbcGrZAHYO3QF6Wu8p8Rl3FGcFnWxFdSSZL1M2k0/7jLi/1+9pRvKPfwhG2g4AIKQQq MAtODm0/T3FxIEiN6lykksd5taIN7Qt/5dLgY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=OIBR/z0fcrgE8vFcxDiErkJ86M8pyGXEc14AMHS5oesWJV1Bk/ReBpQQadePVJO4va 8v0WSzLpZdFZvq50Wqed/SxJeUvz8mznvH/rM8axdVXOXiqTRbJTHeSYij9Ep9oyjXtA w506L5JnnVkjuIs1IoKxiWiD4A0bEol15dyJM= MIME-Version: 1.0 Received: by 10.223.112.130 with SMTP id w2mr2021189fap.65.1232670901129; Thu, 22 Jan 2009 16:35:01 -0800 (PST) In-Reply-To: <560f92640901221458y9409360n34904461fb2580e4@mail.gmail.com> References: <560f92640901221241y4fc1620aree083a812c1f3c8d@mail.gmail.com> <26ddd1750901221333x5356f4f3l6b6410fc05d4e6d4@mail.gmail.com> <560f92640901221451j2e2b259bw1559a8c8d8912941@mail.gmail.com> <560f92640901221458y9409360n34904461fb2580e4@mail.gmail.com> Date: Thu, 22 Jan 2009 19:35:01 -0500 Message-ID: <26ddd1750901221635k17230c7eudb1edc38c808eb83@mail.gmail.com> From: Maxim Khitrov To: Nerius Landys Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org Subject: Re: shell scripting, how to auto-timeout? 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: Fri, 23 Jan 2009 00:35:02 -0000 On Thu, Jan 22, 2009 at 5:58 PM, Nerius Landys wrote: > Actually, because of the "exec" in the parent script, the line below > it, the "killing terminator process" line, never gets reached. So the > terminator process that waits to kill its parent always waits the full > 5 seconds in the background. If I pipe the output of the parent > script through less, it waits 5 seconds before it reaches the end of > the output. That is annoying. Is there any way to solve that? > I see what you mean. Here's another option without exec: #!/bin/sh -T kill_all() { echo 'killing everything' kill $SPID $CPID 2> /dev/null exit 0 } trap kill_all SIGCHLD ./child & CPID=$! sleep 5 & SPID=$! echo "child is $CPID" echo "sleeper is $SPID" wait Here I'm using the fact that the termination of any child process causes SIGCHILD to be sent to the parent. Notice the '-T' in the first line; this is important, because without it the 'wait' call isn't interrupted until both children are dead. Unfortunately, this is a non-standard option, so you'll have to check if it is supported in your environment. - Max