# How to restart server programmatically?

**URL:** <https://www.ruby-forum.com/t/how-to-restart-server-programmatically/90545>\
**Category:** Rails\
**Created:** [March 29, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/how-to-restart-server-programmatically/90545 "2007-03-29T21:18:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [March 29, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/how-to-restart-server-programmatically/90545/1 "2007-03-29T21:18:34Z")

</div>

I need to be able to restart my server based on a specific database  
change.  
I’ve been trying a call to system(“script/process/reaper -a graceful -d  
dispatch.fcgi”) but that raises a transaction error which rolls back the  
save. I wasn’t aware that after\_save would rollback. That’s potentially  
useful but pretty much not right now. Here’s my code:

def after\_update  
system("#{RAILS\_ROOT}/script/process/reaper -a graceful -d  
dispatch.fcgi")  
if routing\_modified?  
end

I’ve also looked at calling the Killer in  
“vendor/rails/railties/lib/commands/process/reaper” but can’t figure out  
how  
to pass it the correct parameters when just _requiring_  
“commands/process/reaper” calls an instance of Killer.

I’d really appreciate your help, kittens. Before I forget, I’m running  
Apache2 [no Mongrel, sadly] and Edge Rails if that makes a difference.

RSL

---

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [April 25, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/how-to-restart-server-programmatically/90545/2 "2007-04-25T22:53:30Z")

</div>

Just in case anyone Googles this looking for a solution, the answer I  
came  
up with is branching off a new Thread in an after\_filter in the  
controller  
and some polite modification of dispatch.fcgi.

[in my controller]  
def ensure\_fresh\_server  
begin  
Thread.new do  
logger.info “restarting server!”  
system “#{RAILS\_ROOT}/script/process/reaper -a graceful”  
end if @site.is\_active? or @site.is\_active\_modified?  
end rescue nil  
end

[dispatch.fcgi]  
#!/usr/bin/env ruby

require File.dirname( **FILE** ) + “/…/config/environment”  
require ‘fcgi\_handler’

class RailsFCGIHandler  
private  
def busy\_exit\_handler(signal)  
dispatcher\_log :info, “busy: asked to terminate during request  
signal  
#{signal}, deferring!”  
@when\_ready = :exit  
end

# Dreamhost sends the term signal and if were handling a request

defer it  
def term\_process\_request(cgi)  
install\_signal\_handler(‘TERM’,method(:busy\_exit\_handler).to\_proc)  
Dispatcher.dispatch(cgi)  
rescue Exception =\> e # errors from CGI dispatch  
raise if SignalException === e  
dispatcher\_error(e)  
ensure  
install\_signal\_handler(‘TERM’, method(:exit\_now\_handler).to\_proc)  
end  
alias\_method :process\_request, :term\_process\_request  
end

RailsFCGIHandler.process!

# End of file…

RSL
