Another possible approach is to use trap to catch the TERM or INT
signals. From there, you can use a variety of strategies to shut the
program down gracefully. You can either hit CTRL-C (or CTRL-D if you
catch TERM) or write a very simple second program that sends the
signal to the first program (start and stop scripts, basically). The
second program will need the process id for the first, which could be
written out to a designated file using $$.
I don't know if trap is supported on Vista. One method of implementing
trap on Windows I've seen (but never used personally) is to wrap the
Kernel's trap method, like this:
def trap(signal)
Kernel::trap(signal){yield}
Thread.new{loop{sleep 1}}
end
You can easily simulate a trap by simply checking a designated file
(you can use $$ to generate a unique filename) for a termination
string (like TERM, ASCII 4) on each iteration of the loop. Then your
shutdown script would simply write the termination string to the file
and exit. The listener script would see the exit string and shut down.
As noted by Ara, this problem is much simpler on Unix, where threading
is better supported. In the next version of Ruby, native threading
will be supported (including on Vista) so hopefully you won't need
these kinds of tricks.
Dan
---
Dan Yoder
http://dev.zeraweb.com/
Ruby And JavaScript Consulting