At times I run a ping command (on GNU/Linux) and it won't stop immediately when I press Ctrl+C.

To my understanding Ctrl+C sends a SIGINT. What are possible causes of the ping command to not exit immediately?

What can I do to make sure it always exits instantly?

1 Answer 1


Programs can bind custom handlers to the SIGINT signal, for example to perform cleanup actions. You can observe this behaviour with this shell script, which binds a handler using trap:

trap "echo Caught!" SIGINT SIGTERM

while true
do sleep 60

To kill an interactive program instantly, you can either do kill -9 <PID> in another shell, or use ctrl-Z to suspend the program and then kill it:

❯ /tmp/test.sh    # Our test script with trap
^CCaught!         # Ctrl-c doesn't work
^Z                # Suspend the process
[1]  + 11713 suspended  /tmp/test.sh
❯ kill -9 %1
[1]  + 11713 killed

Here I'm using %1 to get the pid of this first background process, which is the script because I have no other background processes in this example. You can also use the PID of the process directly, for shells that don't support the %1 syntax.

And that's what ping does, just look at the source code: In the main function, setup is called, which sets signal handlers, including SIGINT. SIGINT is bound to sigexit, which sets a flag to notify the mainloop that ping should exit. This flag is only checked in two places. I guess that one of the system calls used there blocks or takes some time to return, so the flag will never be checked or only after a delay.

  • 1
    You're suggesting that ping binds custom handlers to the SIGINT signal. Still I'd love to know what I can investigate to track down the cause of this behaviour.
    – silviot
    Aug 2, 2013 at 15:52
  • 1
    @silviot I've linked some lines of the ping source code, where the SIGINT handler is bound.
    – bennofs
    Aug 2, 2013 at 16:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .