From: "M. Edward (Ed) Borasky" Date: 2008-07-10T23:40:57+09:00 Subject: Re: Killing a ruby script gracefully --------------080501040103050909090802 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Tim Conner wrote: > 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 > > } 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 --------------080501040103050909090802 Content-Type: text/x-vcard; charset=utf-8; name="znmeb.vcf" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="znmeb.vcf" YmVnaW46dmNhcmQNCmZuOk0uIEVkd2FyZCAoRWQpIEJvcmFza3kNCm46O00uIEVkd2FyZCAo RWQpIEJvcmFza3kNCmVtYWlsO2ludGVybmV0OnpubWViQGNlc21haWwubmV0DQp4LW1vemls bGEtaHRtbDpGQUxTRQ0KdmVyc2lvbjoyLjENCmVuZDp2Y2FyZA0KDQo= --------------080501040103050909090802--