Rails - A server is already running - ruby-on-rails

I have a problem when I want to start my server in my terminal I do rail s or rails server and there is an error A server is already running. Check /Users/baptiste/code/BaptisteB/my-app/tmp/pids/server.pid.
What does it mean? And why it is present?
I delete it and when I check my localhost:3000. There is nothing. I have to log off my laptop and turn on it to begin to work.
How could I stop this error? Maybe I can destroy it with a command. Thank you for your help.

Try to run below command on your terminal and you will get pid (process id)
lsof -wni tcp:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 21309 user 11u IPv4 93186 0t0 TCP *:3000 (LISTEN)
and then kill your ruby process by using
kill -9 <PID>
start your server again by rails s
Hope it helps!

It means that you have already started a rails server. What's possibly happened is that you started a rails server and closed that terminal window without stopping the server. Open a terminal window and run
ps aux | grep rails
that should give you a list of all the processes running with rails in its name. Then you can run the command below to kill all of them or get the pid (process id) and selectively terminate them with the second command. If you're on wi
killall -9 rails
kill pid

Simple:
$ cd # in the project folder
$ gem install shutup
$ shutup
If using rvm do:
$ cd # in the project folder
$ rvm #global do gem install shutup
$ shutup

Related

A server is already running. (Ruby on Rails)

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.

How do I find out and stop server Rails app

I'm currently running a rails app on a remote Debian linux box and need to stop the rails server. Not sure which rails server it is. How do I find out which rails server it is and how to kill it?
ps aux | grep rails
run this in command line then use
pkill -9 rails to kill all the process(rails).
You need to find Rails process ID and kill that process. Use the following code to get the list of rails processes:
ps -ef | grep script/rails
Then, you can kill them by using this:
kill -9 <rails_process_id>

deployed to heroku, can't see project in development

I built a rails app and successfully deployed it to heroku
Now I would like to access the development version to make changes on my local machine
When I rails s I get the error:
A server is already running. Check /home/username/myApp/tmp/pids/server.pid
That file has one line that reads 2673
What can I do to start the rails server while the app is hosted on heroku?
rm /home/username/myApp/tmp/pids/server.pid
Then
rails s
If it is not working
If you want to kill rails server process on port 3000 (which is what webrick normally uses), type this in your terminal to find out the PID of the process:
$ lsof -wni tcp:3000
Then, use the number in the PID column to kill the process:
$ kill -9 PID
Then
rails s
Hope you will be able to run rails server again in your local m/c

Can't stop rails server with CTRL-C

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.

How to restart Rails from within Rails?

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.

Resources