You can catch Interrupt , for example:
pid = fork do begin loop do
You can do the same with Signal.trap('INT') { ... } , but with sleep I think it's easier to catch the exception.
Update: This is a more traditional way of doing this, and it ensures that the loop always ends the full transition before it stops:
pid = fork do stop = false Signal.trap('INT') { stop = true } until stop
The downside is that it will always do sleep , so there will almost always be a delay until the process stops after you kill it. You can probably get around this, sleep in packages, or do a combination of options (saving Interrupt just around sleep or something else).
Theo
source share