From owner-freebsd-questions@FreeBSD.ORG Mon Jul 28 05:57:47 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 24C9E8EC for ; Mon, 28 Jul 2014 05:57:47 +0000 (UTC) Received: from nlpiport08.prodigy.net.mx (nlpiport08.prodigy.net.mx [148.235.52.13]) by mx1.freebsd.org (Postfix) with ESMTP id E60922064 for ; Mon, 28 Jul 2014 05:57:46 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.01,746,1400043600"; d="scan'208";a="59890159" Received: from nlpiport23.prodigy.net.mx ([148.235.52.97]) by nlpiport08.prodigy.net.mx with ESMTP; 28 Jul 2014 00:52:38 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgYTAKXk1VO9qp2K/2dsb2JhbABZgw6BKbANAQEBAQadZIUmCQGBExd3hAQBBVYzCyETEg8qHhmIRL1LF4V8iVcWhDQFinGQWgGUTINpHS8 Received: from dsl-189-170-157-138-dyn.prod-infinitum.com.mx (HELO morena.maps.net) ([189.170.157.138]) by nlpiport23.prodigy.net.mx with SMTP; 28 Jul 2014 00:52:37 -0500 Date: Sun, 27 Jul 2014 22:52:37 -0700 From: Martin Alejandro Paredes Sanchez To: freebsd-questions@freebsd.org Subject: Re: sh 'sleep' and trap? Message-ID: <20140727225237.6d17209f@morena.maps.net> In-Reply-To: <917611ACD1C0644B793C5B81@Mail-PC.tdx.co.uk> References: <917611ACD1C0644B793C5B81@Mail-PC.tdx.co.uk> X-Mailer: Claws Mail 3.10.1 (GTK+ 2.24.22; i386-portbld-freebsd10.0) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Jul 2014 05:57:47 -0000 El Wed, 16 Jul 2014 10:31:22 +0100 Karl Pielorz escribi=F3: >=20 > Hi All, >=20 > I have a script, similar to this: >=20 > " > #!/bin/sh >=20 > echo $$ >/var/run/mypid.pid > trap "rm /var/run/mypid.pid; exit 0" EXIT >=20 > while [ 1 ] > do >=20 > do_stuff > sleep 60 >=20 > done > " >=20 > This works - but an attempt to 'kill' it, e.g. >=20 > kill `head -1 /var/run/mypid.pid` >=20 > Takes up to 60 seconds, before 'sleep' completes, control returns > back to the shell - which see's the signal, and quits. >=20 > Is there a better way of doing this? - i.e. some way the shell can > 'pass time' but still receive signals in a timely manner? >=20 > The only work around I could come up with was to change the 'sleep > 60' into a loop that does 60 * 1 second sleeps, not ideal though :( >=20 > Cheers, >=20 > -Karl Try changing the signal from EXIT to TERM trap "rm /var/run/mypid.pid; exit 0" TERM Try/test the option trapsasync -T trapsasync When waiting for a child, execute traps immediately. If this option is not set, traps are executed after the child exits, as specified in IEEE Std 1003.2 (``POSIX.2''). This nonstandard option is useful for putting guarding shells around children that block signals. The surrounding shell may kill the child or it may just return control to the tty and leave the child alone, like this: sh -T -c "trap 'exit 1' 2 ; some-blocking-program" #!/bin/sh -T