How to stop (and restart) the Rails Server? - ruby-on-rails

I'm following the instructions here http://railsinstaller.org/mac to get up and running with Rails on a Mac running OS X 10.8.2
At step 8 I'm asked to restart Rails server but how?
I'm assuming via a command line, but from within the already open ruby terminal window or a new one?

Now in rails 5 you can do:
rails restart
The output of rails --tasks:
Restart app by touching tmp/restart.txt
I think that is usefully if you run rails as a demon

Press Ctrl+C
When you start the server it mentions this in the startup text.

On OSX, you can take advantage of the UNIX-like command line - here's what I keep handy in my .bashrc to enable me to more easily restart a server that's running in background (-d) mode (note that you have to be in the Rails root directory when running this):
alias restart_rails='kill -9 `cat tmp/pids/server.pid`; rails server -d'
My initial response to the comment by #zane about how the PID file isn't removed was that it might be behavior dependent on the Rails version or OS type. However, it's also possible that the shell runs the second command (rails server -d) sooner than the kill can actually cause the previous running instance to stop.
So alternatively, kill -9 cat tmp/pids/server.pid && rails server -d might be more robust; or you can specifically run the kill, wait for the tmp/pids folder to empty out, then restart your new server.

In case that doesn't work there is another way that works especially well in Windows: Kill localhost:3000 process from Windows command line

if you are not able to find the rails process to kill it might actually be not running. Delete the tmp folder and its sub-folders from where you are running the rails server and try again.

I had to restart the rails application on the production so I looked for an another answer. I have found it below:
http://wiki.ocssolutions.com/Restarting_a_Rails_Application_Using_Passenger

I just reboot the server with
Ctrl + c
That worked for me

Related

Ruby on Rails config change while server is running?

Hi I'm new to Ruby on Rails. I have installed the Testia Tarantula application and am trying to read up on Ruby.
My question is how do I start/stop the server.
For example: I want to change the Admin email, so I execute the following command to change the configuration of the app:
RAILS_ENV=production rake db:config:app
But is this command ok to execute while the server is running, it has 'db' in the command which is what would warn me that I shouldn't run it while the server is up. Anyone have some helpful tips for learning Ruby on Rails server app management?
Welcome to Rails!
You can run rake db:xxxxx while the server is running and it won't hurt anything. However I usually stop my server, run my rake command and then start it back up to ensure that all changes will be picked up. If running in production, I would think you may want to restart the server just to make sure. I believe that the schema is generated/updated upon server startup, just fyi.
As far as starting and stopping the server, if you are attached to it you can just use ctrl + c. If it is detached, you can search for the pid and then kill -9 .
Running rake db:anything will load rails on its own. It doesn't matter if you have a server up or not. This will happen in the background. Think of it as the same as running a sql script while the server is running. It's a separate process.

How to keep server running on EC2 after ssh is terminated

I have an EC2 instance on which I installed rails server. The server also runs fine when I do
rails server
But after I close the ssh connection the server also stops. How can I keep the server running even after closing the ssh connection.
screen rails s
did the trick
after that CTRL + A + D and I left and the server is running fine
Try this. We have to start rails server as daemon.
rails s -d &
run at as server means thu nginx or apache or what ever this development server not mean run as server
user this is need more info https://www.digitalocean.com/community/articles/how-to-install-rails-and-nginx-with-passenger-on-ubuntu
also if want advance sololtion use rubber https://github.com/rubber/rubber
I needed mine running everything not, just rails in the background. Install Screen which makes a sub terminal that isn't affected by your ssh connection. sudo apt-get install screen Open screen screen Then start rails rails server &.
Press 'Crtl + A' then 'D' to escape and type screen -r to get back in to the screen terminal.
I will recommend using apache or something else instead of the regular rails server but you can probably add & at the end and feel free to leave
rails server &
These steps worked for me. MY OS is Description: Ubuntu 16.04.4 LTS
sudo apt-get install screen
screen rails s
CTRL + A + D from terminal to detached the existing process and let it run.
Here's a production proof version using RVM and Systemd. Will keep the server alive if it gets terminated for any reason.
[Unit]
Description=Puma Control
After=network.target
[Service]
Type=forking
User=user
WorkingDirectory=/var/www/your_project_name
PIDFile=/var/www/your_project_name/shared/tmp/pids/puma.pid
ExecStart=/home/user/.rvm/bin/rvm default do bundle exec puma -C /var/www/your_project_name/shared/puma.rb --daemon
ExecStop=/home/user/.rvm/bin/rvm default do bundle exec pumactl -S /var/www/your_project_name/shared/tmp/pids/puma.state -F /var/www/your_project_name/shared/puma.rb stop
Restart=always
# RestartSec=10
[Install]
WantedBy=default.target

Can't stop rails server

I am new to rails and I am using an ubuntu machine and the rubymine IDE. The problem is that I am unable to stop the rails server. I tried to stop the server by killing the rails process. But, when I run pgrep -l rails, no such process is found. So, I am only able to kill ruby processes, but, the server won't stop.
I tried ./script/server stop (since I started it by running ./script/server start), but, that didn't work. Googling around and finding some stackoverflow posts, I tried to change the localhost port's listening port but without success. Could someone help?
You can use other ports like the following:
rails server -p 3001
Normally in your terminal you can try Ctrl + C to shutdown the server.
The other way to kill the Ruby on Rails default server (which is WEBrick) is:
kill -INT $(cat tmp/pids/server.pid)
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:
For example:
$ kill -9 PID
And some of the other answers i found is:
To stop the rails server while it's running, press:
CTRL-C
CTRL-Z
You will get control back to bash. Then type (without the $):
$ fg
And this will go back into the process, and then quit out of Rails s properly.
It's a little annoying, but this sure beats killing the process manually. It's not too bad and it's the best I could figure out.
Updated answer:
You can use killall -9 rails to kill all running apps with "rails" in the name.
killall -9 rails
you can use grep command in following way,
ps aux | grep rails
and then
kill -9 {process_id}
pkill -9 rails to kill all the process of rails
Updated answer
ps aux|grep 'rails'|grep -v 'grep'|awk '{ print $2 }'|xargs kill -9
This will kill any running rails process. Replace 'rails' with something else to kill any other processes.
On my MAC the killall -9 rails does not work. But killall -9 ruby does.
Following are steps to kill server process:
1. lsof -i tcp:3000
2. kill -9 1234
where 1234 is the PID of process: localhost:3000 display in step 1.
OR
Remove file(server.pid) under Rails.root/tmp/pids/ and restart server.
OR
open app in another port by using command:
rails s -p 3001
I generally use:
killall ruby
OR
pkill -9 ruby
which will kill all ruby related processes that are running like rails server, rails console, etc.
1. Simply Delete the pid file from rails app directory
Rails_app -> tmp -> pids -> pid file
Delete the file and run
rails start
2. For Rails 5.0 and above, you can use this command
rails restart
If you are using a more modern version of Rails and it uses Puma as the web server, you can run the following command to find the stuck Puma process:
ps aux | grep puma
It will result in output similar to this:
85923 100.0 0.8 2682420 131324 s004 R+ 2:54pm 3:27.92 puma 3.12.0 (tcp://0.0.0.0:3010) [my-app]
92463 0.0 0.0 2458404 1976 s008 S+ 3:09pm 0:00.00 grep puma
You want the process that is not referring to grep. In this case, the process ID is 85923.
I can then run the following command to kill that process:
kill -9 85923
Use ctrl+c to shutdown your Webrick Server.
Unfortunately if its not works then forcefully close the terminal and restart it.
Another trick is that
1. open your system-monitor(a gui application) on ubuntu
2. Select processes tab
3. Then look for a process having name 'ruby'
4. End that process
Delete the server.pid from tmp/pids folder.
In my case, the error was: A server is already running. Check /home/sbhatta/myapp/tmp/pids/server.pid.
So, I delete server.pid
rm /home/sbhatta/myapp/tmp/pids/server.pid
then run rails s
Ctrl-Z should normally do the trick.
Step 1: find what are the items are consuming 3000 port.
lsof -i:3000
step 2 : Find the process named
For Mac
ruby TCP localhost:hbci (LISTEN)
For Ubuntu
ruby TCP *:3000 (LISTEN)
Step 3: Find the PID of the process and kill it.
kill -9 PID
it's as simple as
pkill -9 ruby
nothing more nothing less
I used killall -9 rails like Sri suggested and it didn't work. I adjusted the command to killall -9 ruby and the server closed immediately.
Tl;dr: killall -9 ruby
When the rails server does not start it means that it is already running then you can start by using new port eg.
rails s -p 3001
or it starts and stops in that case you want to delete temp folder in rails directory structure it starts the rails server.
check the /tmp/tmp/server.pid
there is a pid inside.
Usually, I ill do "kill -9 THE_PID" in the cmd
I have noticed on Windows (Im using 10 but not sure if the same for oler). If you use cmd.exe and ctrl + c the raisl server stops correctly.
However, if you use Git Bash, it doesn't. It says it has but when you look at the tmp pids, its still there.
Maybe a bug with git bash?
killall -9 ruby will kill all the ruby processes, and at-least on my machine, rails servers appear as ruby processes. killall -9 rails is much more specific and doesn't work for older versions of rails servers (it gives a 'rails:no process found' because the process is named ruby)
Encountered this problem a while ago. After submitting a form in activeadmin, the rails server just hanged and I was unable to kill it using normal means (even after ctrl+z it was still running in the background). Learner's answer helped, but this command doesn't need process id.
Follow these steps:
open your project
select in tmp folder
select pids folder
delete server.pid file
now start your rails server
On rails 6 using
ps aux | grep rails was not returning the server process
I had to do
ps aux | grep puma
to find the actual process and then kill it using
kill -9 {process_id}
It is late for this question.
Here is my 2 cents. I made a rake task for stopping the server when I don't have access to it.
I only tested on Mac though.
With this you can simply add it to your project then run the rake command.
Here you go:
Gist link: -latest version will be here.
https://gist.github.com/houmanka/289184ca5d8d92de0499#file-server-rake
Some code in here:
# Make a file under: `project_root/lib/tasks/server.rake`
# Then paste the following code
namespace :server do
desc "Stop the running server by killing the PID"
task :kill do
STDOUT.puts "Enter port number: "
post_number = STDIN.gets.strip
system "pid=$(lsof -i:#{post_number.to_i} -t); kill -TERM $pid || kill -KILL $pid"
end
end
# Then to use it in the terminal: `rake server:kill`
Also, Make sure that you are doing command Cntrl+C in the same terminal (tab) which is used to start the server.
In my case, I had 2 tabs but i forgot to stop the server from correct tab and i was wondering why Cntrl+C is not working.
One super easy way would be
gem install shutup
then go in the current folder of your rails project and run
shutup # this will kill the Rails process currently running
You can use the command 'shutup' every time you want
DICLAIMER: I am the creator of this gem
NOTE: if you are using rvm install the gem globally
rvm #global do gem install shutup
For my windows 10 machine, Ctrl - C + Ctrl - D works.
We can kill rails session on Linux using PORT no
fuser -k 3000/tcp
here 3000 is a port no.
Now restart your server, you will see your server is in running state.
Just open the file using the location given
sudo vi /Users/user1/go/src/github.com/rails_app/rails_project/tmp/pids/server.pid
find the process_id / thread_id at which the process is runnning.
Kill the specified process / thread using kill -9 84699
Press Ctrl - C it will stop
if not check

How to run thin in the vps after the terminal window is closed

Hey guys
After several weeks of local testing, I'm now setting up a VPS, and try to run rails on it. At this point, I can open up a Terminal session and ssh to the VPS, run the thin start, then I the server is running ok, But as soon as I closed the terminal the thin is down.
How can I make thin server running in the VPS all the time?
another question how to change from test mode to production mode in rails.
Thanks
Demonize thin, run it with -d flag.
thin -d
For the task at hand you will want to use a tool called Screens
Install it:
sudo apt-get install screen
Then to run it you run:
screen -d executable
To put screen to background: Ctrl+D
To recall a screen: screen -r.
You should be all good now.
You don't really want to launch and stop thin by hand. You want it to be a daemon to be started when your system start and to be managed like any other daemons (e.g. nginx, syslog, sshd, etc.). How to do this is very distribution-dependent, but you should definitely have a look at /etc/init.d/ or /etc/rc.d/ and /etc/rc.conf.
To go in production mode within the command line you use thin -e production, but the preferred way should be to specify it in thin's configuration files. You should have (or create) an /etc/thin/ folder, with one .yml file for each application you're deploying.

Ruby on Rails: How to start the WEBrick server automatically on Windows in background?

In order to run the my Rails application on Windows XP I open a command line, cd to application's directory, and then run rails server.
I would like to automate this, such that every time I turn on my computer, all I'll have to do is to type localhost:3000 in a browser.
How could I do this ?
The simpler way is to create a batch file with the instruction what you give in the command prompt like
d:
cd projects\myapp
ruby script\server
and then drop a copy of the file to Windows Start -> All Programs -> start up folder.
You have few possibilities to do that.
using the registry you can use HKLM\Software\Microsoft\Windows\CurrentVersion\Run or the better approach would be to create a service, you can see this KB with some instruction how to make a service of whatever executable you want.
have you thought about , AUTOEXEC.BAT or creating some batch files. you create right cmd commands that are run at start up. http://www.aumha.org/a/batches.php
The best approach is turn your application into a service. There is a solution for Mongrel (a web server similar to webrick) called mongrel_service, but is not compatible with Rails 3 (due several changes of Rails internals)
However, you can repurpose mongrel_service codebase to work with thin, another webserver that works with Rails 3.
Please look here where is the only reference to mongrel_service script. changing it to thin start could work.
Perhaps is not the answer you're looking for (as there is some work to be done) but is something :)
start rubyw script/rails server webrick
start -> start in another console
rubyw -> run ruby detached from console

Resources