[REBOL] Re: Signal handling.
From: antonr:iinet:au at: 13-Nov-2003 14:11
At bottom is the post of Carl Sassenrate from October
> Is there any way to handle signals sent to a rebol script? I have this
> script that *MUST* clean-up when receiving a SIGTERM.
Here is a short script that uses the REBOL system-port to intercept
types of interrupts from various operating systems.
Why? For example, you would want to do this if you had a REBOL server
process on Linux that has internal state. You may want the chance to
write out information to files before quitting. This is the code you need
to make it happen.
The code below will detect both CTRL-C and REBOL ESCAPE key.
enable-system-trap: does [
; Trap OS interrupts
if not system/ports/system [
if none? attempt [system/ports/system: open [scheme: 'system]][
print "NOTE: Missing System Port" exit
if find get-modes system/ports/system 'system-modes 'signal [
set-modes system/ports/system [
signal: intersect get-modes system/ports/system 'signal-names [
sigquit sigterm sigint sighup
append system/ports/wait-list system/ports/system
check-system-trap: func [port /local msg] [
; Process OS interrupts
if not port? port [return none]
if any [port/scheme <> 'system port <> system/ports/system][return
if not system/ports/system [return none]
while [msg: pick system/ports/system 1] [
if find [signal escape] msg/1 [
print ["System Trap:" msg]
; (save files here)
wake-port: wait 20 ; timeout period