# Killing a ruby script gracefully

**URL:** <https://www.ruby-forum.com/t/killing-a-ruby-script-gracefully/142679>\
**Category:** Ruby\
**Created:** [July 10, 2008, 10:39am UTC](https://www.ruby-forum.com/t/killing-a-ruby-script-gracefully/142679 "2008-07-10T10:39:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [July 10, 2008, 10:39am UTC](https://www.ruby-forum.com/t/killing-a-ruby-script-gracefully/142679/1 "2008-07-10T10:39:43Z")

</div>

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

}

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [July 10, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/killing-a-ruby-script-gracefully/142679/2 "2008-07-10T16:45:26Z")

</div>

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/](http://ruby-perspectives.blogspot.com/)

“A mathematician is a machine for turning coffee into theorems.” –  
AlfrÃ©d RÃ©nyi via Paul ErdÅ‘s

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [July 10, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/killing-a-ruby-script-gracefully/142679/3 "2008-07-10T18:21:22Z")

</div>

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.
