loxaxs
loxaxs

Reputation: 2279

In bash, how to catch and handle SIGINT, without stopping the script?

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 ?

Upvotes: 4

Views: 3563

Answers (1)

that other guy
that other guy

Reputation: 123400

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.

Upvotes: 7

Related Questions