Ok, so I would like to create an action in Rails to restart itself. I did a little searching and found:
http://snippets.dzone.com/posts/show/5002
Which suggests 2 commands, one to stop and another to restart. The following kills:
ps -a|grep "/usr/local/bin/ruby script/server"|grep -v "grep /usr"|cut -d " " -f1|xargs -n 1 kill -KILL $1
The -HUP signal doesn't restart for me, so I tried to mangle the above command (adjusted so the command worked fine with how I was starting the server under Ubuntu):
ps -eaf|grep "ruby script/server"|grep -v grep|cut -d " " -f3|xargs -n 1 kill -KILL $1;script/server
This works fine in my environment, so I tried to set up an action to execute it:
def restart
fork { exec "ps -eaf|grep \"ruby script/server\"|grep -v grep|cut -d \" \" -f3|xargs -n 1 kill -KILL $1;script/server" }
redirect_to "/server_maintenance"
end
The action kills the server fine, but doesn't actually start the server back up:
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/tcphack.rb:12:in `initialize_without_backlog': Address already in use - bind(2) (Errno::EADDRINUSE)
from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/tcphack.rb:12:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:93:in `new'
from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:93:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb:10:in `new'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb:10:in `run'
from /usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:111
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/server:3
I'm not quite understanding why the address is already in use when Mongrel seems to have just exited.
I found this question:
How do you restart Rails under Mongrel, without stopping and starting Mongrel
but the signals don't cause the restart in my environment, they just end up killing the process.
Anyone have any ideas on what may work? For some notes on my environment: I installed Rails from a new version of RubyGems, and Mongrel. I use script/server to start the server, which of course uses Mongrel. I'm on Ubuntu Hardy Heron.
If you don't mind switching to mod_rails, you can restart your server by creating $RAILS_ROOT/tmp/restart.txt, which causes only the Rails instance you care about to restart.
Your PS command looks (cursorary glance) like it will kill all rails processes on your box. That's fine if you are the only Rails app on a machine, but if there's a few running as the same user or you are running as root you'll kill them all. Bad form!
This points it out for mongrel. There's the way you want to try.
Ok I found a fix... I changed how I start rails to:
mongrel_rails start -d
and now the following action will do it:
def restart
fork { exec "mongrel_rails restart" }
redirect_to "/server_maintenance"
end
As a caveat, the redirect_to will cause a failed load because the server will be down... however a reload after a pause will show that the restart was successful. This could be fixed by changing the restart to be done with AJAX, followed by a javascript reload... but I will leave that as an exercise to the reader.
In our consulting with startups running their sites on Rails, we used two methods for managing mongrel processes.
First, is a custom gem we wrote called mongrel_runit. This sets mongrels up as services in runit.
Second, we used god to monitor mongrel processes. This will work with mongrel_runit, or with 'normal' mongrel configurations.
Related
I was practicing doing ruby on rails and I accidentally closed Visual Studio code while the terminal and rails server is running.
This is what happened after i typed rails s
PS C:\Users\Lenovo\Desktop\folder_name\project_name rails s
=> Booting Puma
=> Rails 6.1.4.1 application starting in development
=> Run `bin/rails server --help` for more startup options
A server is already running. Check C:/Users/Lenovo/Desktop/folder_name/project_name/tmp/pids/server.pid.
Exiting
Find the process id for the rails server port :
lsof -wni tcp:3000
Copy value in PID column
Kill the process (for example 14400) :
kill -9 14400
You can use this command to kill the server using port number.
sudo kill -9 $(lsof -i :3000 -t)
The user just needed to delete the server.pid file and it worked.
I setup Nginx for listening to lockalhost:3000 than I launch rails command bundle exec rails server webrick -e production. I found that I can launch rails server as daemon simply adds the -d flag to the command, so the command becomes a bundle exec rails server -d webrick -e production. My problem is that after server reloads or app is crushed - that a dead-end, I can't found info about how should I create "rails as a daemon with auto relaunch".
webrick in production?
Please please please refrain from doing anything like that. Use puma or unicorn or any similar app server for your purpose.
And for the process monitoring part, you can use systemd, or monit for better control.
Personally, I prefer monit as it gives me crash logs and downtime alerts.
I'm new to Ruby and Rails. I start the WEBrick Server from scripts/server (via ./scripts/server from the directory created by rails) on Debian. The Server starts and is reachable, but if I press CTRL + C then appears
ERROR SystemExit: exit
[rails dir]/vendor/rails/railties/lib/commands/server.rb:106:in `exit'
and the Server won't stop. What goes wrong?
*nix
First step, lookup the process ID (PID) of rails server; you'll need the port it's running on.
Second step, manually kill the process using the PID obtained in step one.
sudo lsof -i tcp:<PORT> # e.g. 3000
kill -9 <PID> # e.g. 14319
For any latecomers, Rails 2.3.8 doesn't like Rack 1.2.1
Add gem 'rack', '1.1.0' to your gemfile, run bundle update rack and your server should exit correctly.
Try to find the process with ps aux in your terminal.
Then, kill -9 it.
Running Ubuntu Server 10.04 with Rails 2.3.4 and Webrick 1.3.1; our rails app runs fine when called via script/server -e production, but trying to test it as a daemon by calling it with the -d flag produces the following output:
=> Booting WEBrick
=> Rails 2.3.4 application starting on http://0.0.0.0:3000
Nothing is produced in the logs, and other Rails applications will run detached without issue.
I assume You are running the Webrick in port 3000
>>$ sudo netstat -anp | grep 3000
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 8822/ruby
>>$ sudo kill -9 8822
I don't mean to contradict your choosing Webrick as a production server and maybe there is something I'm missing about why you are choosing Webrick, but have you considered other alternatives? I'd wager you already know all of this, but Webrick is the provided ruby server, and it is also the slowest ruby server choice.
Some of the most popular production server choices are:
Passenger
Thin
Mongrel
Unicorn
Glassfish
Passenger is likely the most popular choice for production now due to its easy configuration, speed, and features.
If there is a specific use case for Webrick that makes it better than any of the other server choices, I'd love to know.
You can add patch to enable error log here: https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/core_ext/process/daemon.rb#L16
To
unless noclose
STDIN.reopen "/dev/null" # Free file descriptors and
STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
STDERR.reopen '/tmp/rails_daemon_err.log', 'a'
end
Now when you start rails server with -d, the error log will append to /tmp/rails_daemon.log.
I'm running Teambox (a Ruby on Rails app) and have the server running with:
script/server -e production
Knowing absolutely nothing about Ruby on Rails I just wandered how I could restart the server to get it to update changes I've made to the config?
If you ran it with that command line it's probably running actively in the console, just CTRL+C.
If you ran it daemonize you will need to find the process and kill it.