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 Signal::INT.trap do puts "Gotcha!" end puts "Ctrl+C will be caught from now on" sleep 3 Signal::INT.reset puts "Ctrl+C is back to the OS default action" sleep 3
Note:
1
2
3
4
5
6
6
8
9
7
11
31
13
14
15
23
19
20
18
17
21
22
29
24
25
26
10
12
28
30
16
31
Clears the handler for this signal and prevents the OS default action.
Resets the handler for this signal to the OS default.
Sets the handler for this signal to the passed function.
Enum
Enum
Enum
Comparable(Enum)
Value
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 thought 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.
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/Signal.html