From owner-freebsd-questions@FreeBSD.ORG Wed Feb 7 16:46:36 2007 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 4EE8416A400 for ; Wed, 7 Feb 2007 16:46:36 +0000 (UTC) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.freebsd.org (Postfix) with ESMTP id 1267F13C481 for ; Wed, 7 Feb 2007 16:46:35 +0000 (UTC) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (dan@localhost [127.0.0.1]) by dan.emsphone.com (8.14.0/8.13.8) with ESMTP id l17GkY5P072473 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 7 Feb 2007 10:46:34 -0600 (CST) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.14.0/8.14.0/Submit) id l17GkYev072469; Wed, 7 Feb 2007 10:46:34 -0600 (CST) (envelope-from dan) Date: Wed, 7 Feb 2007 10:46:34 -0600 From: Dan Nelson To: Aitor San Juan Message-ID: <20070207164634.GL37689@dan.emsphone.com> References: <6FA4E8E8A0FAD64F9AF5A1F0FDB8C6EE1211@BB06.bolsabilbao.local> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-OS: FreeBSD 6.2-STABLE User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: ClamAV 0.88.7/2533/Wed Feb 7 08:20:47 2007 on dan.emsphone.com X-Virus-Status: Clean Cc: freebsd-questions@freebsd.org Subject: Re: Trapping signal from shell script... doesn't seem to work? 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: Wed, 07 Feb 2007 16:46:36 -0000 In the last episode (Feb 07), Aitor San Juan said: > I have written a Bourne shell script. This shell script invokes a > program written in the C language. > > Below is basically the shell script source code. As you can see, the > C program is not invoked in the background, but in the foreground, so > the shell script doesn't finish until the C program has finished. > > I want the shell script to trap TERM or INT signals, so when any of > these are raised, the shell script will try to send SIGTERM to the > program "myprog": Since you didn't background "myprog", the shell can't do anything until it returns, including signal processing. See the text at http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_11 There's a nice page about trap handling and shells at http://www3.cons.org/cracauer/bourneshell.html , which includes an example that does what you want: #! /bin/sh pid= onint() { kill $pid } trap onint SIGINT ./hardguy & pid=$! wait $pid -- Dan Nelson dnelson@allantgroup.com