[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] How to trap SIGTERM?

From: Pierre Gaston
Subject: Re: [Help-bash] How to trap SIGTERM?
Date: Sat, 20 Apr 2013 08:17:21 +0300

On Sat, Apr 20, 2013 at 7:27 AM, Peng Yu <address@hidden> wrote:

~/linux/test/bash/man/builtin/trap/SIGTERM$ cat
#!/usr/bin/env bash

echo $$
trap "echo Booh!; exit" SIGTERM
while true
  sleep 60

I have the above script. I run it and get the process id. Then I use
"kill -SIGTERM pid" to kill it. But the script fails to respond. Does
anybody know how to get the script respond to SIGTERM? Thanks.


If you wait a bit you will see that the script does respond to SIGTERM.
The relevant bit of the manual is:

If  bash is waiting for a command to complete and receives a signal for
which a trap has been set, the trap will not be executed until the com‐
mand  completes.   When bash is waiting for an asynchronous command via the wait builtin, the reception of a signal for which a trap  has  been
set will cause the wait builtin to return immediately with an exit sta‐
tus greater than 128, immediately after which the trap is executed.

That is echo Booh! will be executed after sleep exits and a workaround if you want to see "echo Booh!" "immediately" is to do: sleep 60 & wait $!

Note that sleep will not be killed in this case either.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]