Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Signal handling.

From: antonr:iinet:au at: 13-Nov-2003 14:11

At bottom is the post of Carl Sassenrate from October last year. Anton.
> Hello. > > Is there any way to handle signals sent to a rebol script? I have this > script that *MUST* clean-up when receiving a SIGTERM. > > Thanks. > > -Bruno
Here is a short script that uses the REBOL system-port to intercept shutdown 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 ] ] ] system/console/break: 'signal 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 port] 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) quit ] ] none ] print "Starting..." enable-system-trap forever [ wake-port: wait 20 ; timeout period check-system-trap wake-port ]