Killing a ruby script gracefully

I have a Ruby script that uses Event machine to listen on a port for
incoming data. When data is received, the script saves it to the
database.
How can I kill this script gracefully? I want to make sure that the
data is not being processed or saved the to DB when I kill it.

script looks something like:

#!/usr/bin/env ruby
require ‘rubygems’
require ‘eventmachine’

module TestServer
def receive_data data
#Process and save data
end

end

EventMachine::run {
$a = EventMachine::open_datagram_socket “”, 1720,
TestServer

}

Tim C. wrote:

require ‘eventmachine’
TestServer

}

I don’t know about Windows, but on Linux, and most Unix-ish systems,
when you “kill” a process, what you are actually doing is sending a
signal to it, which the process can handle, catch or ignore. Processes
usually have a default behavior for some signals built in. So check out
“Signal handling” in the Pickaxe for the details. And if you have a
Linux system, do “man kill” and “kill -l” for the low-level details.

One note: signal number 9 can not be caught or ignored on a Unix-ish
box. So even if you write your own signal handler, anybody with the
right privileges can send signal 9 to it and force an immediate and
possibly data-corrupting end to your script.


M. Edward (Ed) Borasky
http://ruby-perspectives.blogspot.com/

“A mathematician is a machine for turning coffee into theorems.” –
Alfréd Rényi via Paul Erdős

M. Edward (Ed) Borasky wrote:

require ‘rubygems’
$a = EventMachine::open_datagram_socket “”, 1720,

One note: signal number 9 can not be caught or ignored on a Unix-ish
box. So even if you write your own signal handler, anybody with the
right privileges can send signal 9 to it and force an immediate and
possibly data-corrupting end to your script.

Sounds like he was asking: which signals does EM handle and how…?

I would guess TERM would be handled with a graceful exit, just because
that’s fairly standard, but ask someone who knows EM better than I do.