tags:

views:

62

answers:

1

If ruby myapp.rb starts sinatra previewing at localhost:4567, how can I programatically stop/halt/kill it? Terminal command (other than Ctrl-C), or Rake tasks would be fine.

I need to incorporate this into a Rake task or terminal.

Thanks

+2  A: 

In myapp.rb, add this before sinatra starts:

puts "This is process #{Process.pid}"

When you want to kill it, do this in a shell:

kill <pid>

Where <pid> is the number outputted by myapp.rb. If you want to do it in ruby:

Process.kill 'TERM', <pid>

Both of these will let sinatra run it's exit routine. If you don't want to type in the pid every time, have myapp.rb open a file and put it's pid in it. Then when you want to stop it, read the file and use that. Example:

# myapp.rb:
File.open('myapp.pid', 'w') {|f| f.write Process.pid }

# shell:
kill `cat myapp.pid`

# ruby:
Process.kill 'TERM', File.read('myapp.pid')
Adrian
Thanks, expecially for the last code - which I'm using. I needed to convert to int for `Process.kill 'TERM', File.read('myapp.pid').to_i`
Dr. Frankenstein