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

2007-02-07 Thread Dan Nelson
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
[EMAIL PROTECTED]
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


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

2007-02-07 Thread Aitor San Juan
Dan, thanks a lot. I'll have a look at it.

Regards.

-Mensaje original-
De: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] nombre de Dan Nelson
Enviado el: miércoles, 07 de febrero de 2007 17:47
Para: Aitor San Juan
CC: freebsd-questions@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
[EMAIL PROTECTED]


 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.
**
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


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

2007-02-07 Thread Derek Ragona
I believe what you are seeing is the login shell you are running holding 
back the signal as it first traps the signals.  You should try running the 
script non-interactively from cron or nohup.


-Derek


At 09:18 AM 2/7/2007, Aitor San Juan wrote:

Hi list!

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:

user1:/usr/home/user1$ ps -ax | grep -v grep | grep myprog
  PID  TT  STAT  TIME COMMAND
22406  p0  I+ 0:00.01 /bin/sh ../cronjobs/myshell.sh
22449  p0  I+ 0:00.00 /usr/home/user1/myprog -d

user1:/usr/home/user1$ kill -TERM 22406

user1:/usr/home/user1$ ps -ax | grep -v grep | grep myprog
  PID  TT  STAT  TIME COMMAND
22406  p0  S+ 0:00.01 /bin/sh ../cronjobs/myshell.sh
22449  p0  I+ 0:00.00 /usr/home/user1/myprog -d

I notice the change in state of the shell script process from I+
to S+, but nothing else happens. The shell script seems to remain
in execution. However as soons as I press Ctrl-C at the terminal
where I invoked the shell script, it indeed receives the Ctrl-C,
but myprog receives it first so when the function trap_handler
executes, there is no myprog process in memory.

My question: Can anybody tell me what happens and/or what am I
doing wrong? Why must I press Ctrl-C to force the shell script to
finally receive the TERM signal when I executed the kill command
from another session? What is this apparent delay due to?

Thanks in advance.

#-- SHELL SCRIPT BEGIN --
#!/bin/sh
#
trap_handler() {
echo *** SYSTEM SIGNAL RECEIVED ***
echo $1 caught. Ending...
pid=`find_myprog's_PID`
kill -TERM $pid
exit 1
}

trap 'trap_handler SIGINT' INT
trap 'trap_handler SIGTERM' TERM

/usr/home/user1/myprog -d
#-- SHELL SCRIPT END --


 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.

**
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
MailScanner thanks transtec Computers for their support.


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
MailScanner thanks transtec Computers for their support.

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]