On 10/14/2011 09:55, luke gruber wrote:
>> trap an OS signal that the host is shutting down, allowing Ruby to tidy >some
> things up first.
>
> For unix-like OS's, when the computer is shutting down all processes are
> sent a SIGTERM, so if you use the the trap method (defined in the Kernel
> module) like this,
>
> trap("TERM") do
> ...important stuff...
> ...
> end
FYI, trapping TERM is not a guaranteed check that the host is shutting
down. It's just a signal sent to programs to kill them that happens to
*also* be used by the usual shutdown process.
> it should work. For other OS's, just find out what signals are sent on
> shutdown, and see if they match the signals recognized by your Ruby
> version by matching them against the output of Signal.list().
Windows doesn't use signals in the same sense as Unix. It sends
messages which are sort of the same, but you have to use an entirely
different mechanism to trap them. You'll need to get chummy with some
gems for Windows or the Win32API module or equivalent for Ruby 1.9.
Does it make a difference for your program to clean up only on system
shutdown as opposed to program exit time (whenever that happens)? If
you just want to do the cleanup chore any time the program exits
(including system shutdown), you might simply be able to use the at_exit
method:
at_exit { puts "I'm dying!" }
puts "I'm alive!"
sleep 10
That should print "I'm alive!", wait for 10 seconds, and then print "I'm
dying!". If you instead hit Ctrl-C or send a TERM signal to the program
before the 10 seconds is up, it will still print "I'm dying". The last
message won't be printed if the KILL signal is sent however.
I haven't tried to see if this would work during a Windows shutdown
event, but it should work on most Unix systems. If it doesn't work on
Windows or if you really must know that it's a system shutdown event
specifically, you may need to resort to triggering your cleanup logic to
run at shutdown as a separate program. This looks like a way to do that
in Windows:
http://www.computing.net/answers/windows-xp/run-program-on-shutdown/6762.html
I think you would configure launchd on OSX to do something similar. For
Linux systems it will depend on the init system being used for the
distribution in question.
-Jeremy