Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 27 Jul 2014 22:52:37 -0700
From:      Martin Alejandro Paredes Sanchez <mapsware@prodigy.net.mx>
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>

next in thread | previous in thread | raw e-mail | index | archive | help
El Wed, 16 Jul 2014 10:31:22 +0100
Karl Pielorz <kpielorz_lst@tdx.co.uk> 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



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