onintr - macOS


Overview

onintr allows users to specify the action the system should take when a user presses Ctrl+C. By default, Ctrl+C sends an INT signal to terminate the current process, but onintr lets users choose a different action, such as quitting the current shell or just ignoring the signal.

Syntax

onintr [-q] [-s SIGNAL]

Options/Flags

-q

Quit the current shell when Ctrl+C is pressed.

-s SIGNAL

Specify the signal to send when Ctrl+C is pressed. The default is INT. Common signals include:

  • CONT: Continue the process.
  • INT: Terminate the process.
  • QUIT: Send the SIGQUIT signal, which typically causes a core dump.

Examples

Quit the current shell when Ctrl+C is pressed

onintr -q

Send the CONT signal when Ctrl+C is pressed

onintr -s CONT

Send the QUIT signal when Ctrl+C is pressed

onintr -s QUIT

Common Issues

onintr does not affect the behavior of Ctrl+Z

Ctrl+Z sends the TSTP signal by default, which suspends the current process. onintr only affects the behavior of Ctrl+C (the INT signal).

Ctrl+C does not terminate the process immediately

Some programs may ignore the INT signal and continue running. To force quit a program, use Ctrl+D to send the EOF signal or use the kill command with the appropriate signal.

Integration

onintr can be used in scripts to control the behavior of Ctrl+C. For example, to ignore Ctrl+C in a script:

#!/bin/bash
onintr -s CONT
  • kill: Send a signal to a process.
  • stty: Set terminal settings, including the behavior of Ctrl+C.