If you look at the run! method run! in the sinatra source in base.rb, you will see the following:
def run!(options={}) ... handler.run self, :Host => bind, :Port => port do |server| [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } } set :running, true end ... end
There is no way to connect callbacks. BUT! As you can see, the :running parameter changes after the server shuts down.
So, a simple solution is like viewing the App.settings.running stream in a short polling cycle (every 500 ms or something in this series). Once running true, you can safely do your job.
Edit: An improved version, with a little monkey fix. Adding the after_running callback to Sinatra:
class Sinatra::Base
Casper
source share