From owner-freebsd-questions@FreeBSD.ORG Wed Feb 7 22:08:14 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 CA2DE16A401 for ; Wed, 7 Feb 2007 22:08:14 +0000 (UTC) (envelope-from derek@computinginnovations.com) Received: from betty.computinginnovations.com (dsl081-227-250.chi1.dsl.speakeasy.net [64.81.227.250]) by mx1.freebsd.org (Postfix) with ESMTP id 0BE6F13C467 for ; Wed, 7 Feb 2007 22:08:11 +0000 (UTC) (envelope-from derek@computinginnovations.com) Received: from p28.computinginnovations.com (dhcp-10-20-30-100.computinginnovations.com [10.20.30.100]) (authenticated bits=0) by betty.computinginnovations.com (8.13.8/8.12.11) with ESMTP id l17M7aWH035399; Wed, 7 Feb 2007 16:07:37 -0600 (CST) Message-Id: <6.0.0.22.2.20070207160529.027c8ab0@mail.computinginnovations.com> X-Sender: derek@mail.computinginnovations.com X-Mailer: QUALCOMM Windows Eudora Version 6.0.0.22 Date: Wed, 07 Feb 2007 16:07:23 -0600 To: "Aitor San Juan" , From: Derek Ragona In-Reply-To: References: <6FA4E8E8A0FAD64F9AF5A1F0FDB8C6EE1211@BB06.bolsabilbao.local> Mime-Version: 1.0 X-ComputingInnovations-MailScanner-Information: Please contact the ISP for more information X-ComputingInnovations-MailScanner: Found to be clean X-ComputingInnovations-MailScanner-From: derek@computinginnovations.com X-Spam-Status: No Content-Type: text/plain; charset="iso-8859-1"; format=flowed Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: 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 22:08:14 -0000 I believe what you are seeing is the login shell you are running holding=20 back the signal as it first traps the signals. You should try running the= =20 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=3D`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= =20 >babestuta dagoena. Zuri zuzendua ez bada, bidali duenari esan eta ezabatu,= =20 >inori berbidali edo gorde gabe, legeak debekatzen duelako mezuak=20 >erabiltzea baimenik gabe. >-------------------------------------------------------------------------- >Este mensaje puede contener informaci=F3n confidencial, en propiedad o=20 >legalmente protegida. Si usted no es el destinatario, le rogamos lo=20 >comunique al remitente y proceda a borrarlo, sin reenviarlo ni=20 >conservarlo, ya que su uso no autorizado est=E1 prohibido legalmente. >-------------------------------------------------------------------------- >This message may contain confidential, proprietary or legally privileged= =20 >information. If you are not the intended recipient of this message, please= =20 >notify it to the sender and delete without resending or backing it, as it= =20 >is legally prohibited. >************************************************************************** >_______________________________________________ >freebsd-questions@freebsd.org mailing list >http://lists.freebsd.org/mailman/listinfo/freebsd-questions >To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.or= g" > >-- >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. --=20 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.