Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop sinatra from running?

Tags:

ruby

sinatra

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.

like image 741
Dr. Frankenstein Avatar asked Jul 07 '10 17:07

Dr. Frankenstein


2 Answers

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')
like image 83
Adrian Avatar answered Sep 18 '22 12:09

Adrian


In OS X, from the command line (Terminal.app, or DTerm) just enter:

$ killall ruby

every ruby process will stop. Sinatra too.

In Linux (and other UNIXes), you can:

$ ps aux | grep ruby
$ kill <ruby-process-id>
like image 23
Diego Freniche Avatar answered Sep 18 '22 12:09

Diego Freniche