Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 7 Feb 2007 10:46:34 -0600
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Aitor San Juan <asanjuan@bolsabilbao.es>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Trapping signal from shell script... doesn't seem to work?
Message-ID:  <20070207164634.GL37689@dan.emsphone.com>
In-Reply-To: <D44BEB355E6EC14C9B825F8C956E12E1E4C1@BB06.bolsabilbao.local>
References:  <6FA4E8E8A0FAD64F9AF5A1F0FDB8C6EE1211@BB06.bolsabilbao.local> <D44BEB355E6EC14C9B825F8C956E12E1E4C1@BB06.bolsabilbao.local>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070207164634.GL37689>