4

It looks like

trap on_sigint SIGINT

just stop the script as soon as SIGINT is caught. Then, on_sigint is executed. Is it possible to handle SIGINT without stopping the script ?

loxaxs
  • 2,149
  • 23
  • 27
  • What does your `on_sigint` do? Can you show your code? The script will only stop if your handler for SIGINT exits the script. – lurker Jan 19 '14 at 00:48
  • The function is : `on_SIGINT () { echo '<< SIGINT received >>' ; stop=1 ; echo 'Will stop.'}` – loxaxs Jan 19 '14 at 00:59
  • You know that script names are case sensitive, right? So if your interrupt handler is named `on_SIGINT` and you are doing `trap on_sigint`, then your trap won't be executed. You should add your code to your problem description. And is there some other part of the script looking at `stop` and exiting if it is `1`? – lurker Jan 19 '14 at 01:03
  • Sorry for the case, I mis-copied. The stop is supposed to be used to break a loop with `[[ -n $stop ]] && break`, but we are going away from the question. – loxaxs Jan 19 '14 at 01:37
  • see also [Catch SIGINT in bash, handle AND ignore](https://stackoverflow.com/questions/15785522/catch-sigint-in-bash-handle-and-ignore) – milahu Oct 24 '22 at 07:43

1 Answers1

7

SIGINT does not kill the script after the handler runs. Here is a small, self contained test case:

trap on_sigint SIGINT
on_sigint() { echo "caught"; }

{ sleep 3; kill -SIGINT $$; } &

echo "Waiting for sigint"
sleep 5
echo "Still running"

The output is:

Waiting for sigint
caught
Still running

If your observation was correct, the last line would not have been there.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 1
    You are right, the problem seems to happend only when pressing ^C to interrupt the command. It looks like it first interrupt the running command of the script, then handle the signal. – loxaxs Jan 19 '14 at 02:25