Trapping signal from shell script... doesn't seem to work?

Aitor San Juan asanjuan at bolsabilbao.es
Wed Feb 7 16:59:01 UTC 2007


Dan, thanks a lot. I'll have a look at it.

Regards.

-----Mensaje original-----
De: owner-freebsd-questions at freebsd.org
[mailto:owner-freebsd-questions at freebsd.org]En nombre de Dan Nelson
Enviado el: miércoles, 07 de febrero de 2007 17:47
Para: Aitor San Juan
CC: freebsd-questions at freebsd.org
Asunto: Re: Trapping signal from shell script... doesn't seem to work?


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 at allantgroup.com


************ LEGEZKO OHARRA / AVISO LEGAL / LEGAL ADVICE ************* 
Mezu honek isilpeko informazioa gorde dezake, edo jabea duena, edota legez babestuta dagoena. Zuri zuzendua ez bada, bidali duenari esan eta ezabatu, inori berbidali edo gorde gabe, legeak debekatzen duelako mezuak erabiltzea baimenik gabe. 
--------------------------------------------------------------------------
Este mensaje puede contener información confidencial, en propiedad o legalmente protegida. Si usted no es el destinatario, le rogamos lo comunique al remitente y proceda a borrarlo, sin reenviarlo ni conservarlo, ya que su uso no autorizado está prohibido legalmente.
--------------------------------------------------------------------------
This message may contain confidential, proprietary or legally privileged information. If you are not the intended recipient of this message, please notify it to the sender and delete without resending or backing it, as it is legally prohibited.
**************************************************************************


More information about the freebsd-questions mailing list