Safely handle inter-process signals on POSIX systems.
Signals are dispatched to the event loop and later processed in a dedicated fiber. Some received signals may never be processed when the program terminates.
puts "Ctrl+C still has the OS default action (stops the program)" sleep 3.seconds Signal::INT.trap do puts "Gotcha!" end puts "Ctrl+C will be caught from now on" sleep 3.seconds Signal::INT.reset puts "Ctrl+C is back to the OS default action" sleep 3.seconds
WARNING An uncaught exception in a signal handler is a fatal error.
The set of available signals is platform-dependent. Only signals that exist on the target platform are available as members of this enum.
ABRT, FPE, ILL, INT, SEGV, and TERM are guaranteed to exist on all platforms.PWR, STKFLT, and UNUSED only exist on Linux.BREAK only exists on Windows.The methods #trap, #reset, and #ignore may not be implemented at all on non-POSIX systems.
The standard library provides several platform-agnostic APIs to achieve tasks that are typically solved with signals on POSIX systems:
Process.on_terminate.TERM or KILL signal to a process is Process#terminate.Process::Status#exit_signal) is Process::Status#exit_reason.2 4 8 11 15 6 1 3 5 6 9 7 31 13 14 23 19 20 18 17 21 22 29 24 25 26 10 12 28 30 16 31 Returns true if this enum value equals ABRT
Returns true if this enum value equals ALRM
Returns true if this enum value equals BUS
Returns true if this enum value equals CHLD
Returns true if this enum value equals CONT
Returns true if this enum value equals FPE
Returns true if this enum value equals HUP
Clears the handler for this signal and prevents the OS default action.
Returns true if this enum value equals ILL
Returns true if this enum value equals INT
Returns true if this enum value equals IO
Returns true if this enum value equals IOT
Returns true if this enum value equals KILL
Returns true if this enum value equals PIPE
Returns true if this enum value equals PWR
Returns true if this enum value equals QUIT
Resets the handler for this signal to the OS default.
Returns true if this enum value equals SEGV
Returns true if this enum value equals STKFLT
Returns true if this enum value equals STOP
Returns true if this enum value equals SYS
Returns true if this enum value equals TERM
Sets the handler for this signal to the passed function.
Returns true if this enum value equals TRAP
Returns any existing handler for this signal
Returns true if this enum value equals TSTP
Returns true if this enum value equals TTIN
Returns true if this enum value equals TTOU
Returns true if this enum value equals UNUSED
Returns true if this enum value equals URG
Returns true if this enum value equals USR1
Returns true if this enum value equals USR2
Returns true if this enum value equals VTALRM
Returns true if this enum value equals WINCH
Returns true if this enum value equals XCPU
Returns true if this enum value equals XFSZ
Enum
Enum
Enum
Enum
Comparable(Enum)
Value
Object
Object
Object
Clears the handler for this signal and prevents the OS default action.
Note that trying to ignore CHLD will actually set the default crystal handler that monitors and reaps child processes. This prevents zombie processes and is required by Process#wait for example.
Resets the handler for this signal to the OS default.
Note that trying to reset CHLD will actually set the default crystal handler that monitors and reaps child processes. This prevents zombie processes and is required by Process#wait for example.
Sets the handler for this signal to the passed function.
After executing this, whenever the current process receives the corresponding signal, the passed function will be called (instead of the OS default). The handler will run in a signal-safe fiber throughout the event loop; there is no limit to what functions can be called, unlike raw signals that run on the sigaltstack.
Note that CHLD is always trapped and child processes will always be reaped before the custom handler is called, hence a custom CHLD handler must check child processes using Process.exists?. Trying to use waitpid with a zero or negative value won't work.
NOTE Process.on_terminate is preferred over Signal::INT.trap as a portable alternative which also works on Windows.
Returns any existing handler for this signal
Signal::USR1.trap { }
prev_handler = Signal::USR1.trap_handler?
Signal::USR1.trap do |signal|
prev_handler.try &.call(signal)
# ...
end
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/Signal.html