Rails server start/stop - ruby-on-rails

To run the Rails server, I use $rails server. It says 'To stop, click Ctrl+c'.
I use Putty.
The questions are:
Should I keep the terminal open always? Because if the server stops, my web page wouldn't work. With Apache I just used commands apachectl start/stop.
What if I want to use a command? Should I stop the server, use command, and run again? Because in the same terminal I can't do enything if the server run.

you can run it in background by writing
daemonize true
in your puma.rb file
To stop you need to find your running puma process
ps aux | grep puma
then you need to kill the main process
sudo kill -9 your process id
to start you need to type
puma -C config/puma.rb

You can start a daemonized server by adding -d to your command. For instance:
rails server -d
To stop the server, you can kill it based on its process id:
kill $(cat tmp/pids/server.pid)

Should I keep the terminal open always? Because if the server stops, my web page wouldn't work. With Apache I just used commands apachectl start/stop.
Yes, you should keep it open because if you Ctrl C it will stop the server. Another option is to run it in the background but I'm not sure how to do that in Putty.
What if I want to use a command? Should I stop the server, use command, and run again? Because in the same terminal I can't do enything if the server run.
Can you open another terminal window? If you have two terminal windows you can use one for running the server and another for other tasks.

Related

Unable to start rails server

I am trying to start rails server using rails s command. But i'm getting the following error:
A server is already running. Check repo/tmp/pids/server.pid.
I checked the file, but its blank. There is no id in the server.pid file. Then I tried to find out the process running on port 3000 using the command lsof -wni tcp:3000. It also does not show any id.
Then I tried starting the server on a different port using rails s -p 3001, but this also gives the same error. Please could someone explain what is going on.
Thanks
EDIT: does not work even after restarting computer
Just remove the file then:
rm repo/tmp/pids/server.pid
Try running this command in your terminal window.
kill -9 $(lsof -i tcp:3000 -t)
If it doesn't work, you can always just restart your computer/server...
1- Use the command below for checking that how many server currently running
$ ps aux | grep rails
2- For killing the existing server
$ kill -9 pid

Unable to start webbrick again without restarting ec2 instance

I am running a rails 3.2 application on amazon ec2 in development environment and detach mode.
$ rails s -d
After this command the ec2 terminal hangs and does not come out of this command but the server starts as I can access the application.I have to close the terminal and the server remains started.
After this I kill the application.
$ lsof|grep 3000
$ kill -9 <pid>
Now if I try to restart the server, it gives error.
A server is already running. Check /home/ubuntu/trade_ship/tmp/pids/server.pid.
Exiting
Now even if I delete the tmp folder and recreate it, the server won't start. Can anyone help me with these two issues?
Even I had faced that issue, try to restart your system and then check.. this solution worked for me at that time.
First of all if you are not able to use port 3000 use rails s -p <port no> command
Second is if you have to kill the RUBY instance which server started so use
ps aux | grep ruby
username 17731 0.1 1.6 3127008 67996 ?? S 2:00PM 0:01.42 /Users/username/.rvm/rubies/ruby-1.9.2-p180/bin/ruby script/rails s -d
and then kill
kill -9 17731
This will definitely solve the issue

Server is already running in Rails

When I am starting rails server using rails s command it is showing A server is already running. Check C:/Sites/folder/Pids/Server.pids
When I open the file it is outputting a 4 digit number only so how could I resolve this issue ?
FYI
No other instance of Rails cmd is running this time.
Checked Task manager but only cmd.exe is showing no else process is running. (using Windows).
TL;DR Just Run this command to Kill it
sudo kill -9 $(lsof -i :3000 -t)
Root Cause:
Because PID is locked in a file and web server thinks that if that file exists then it means it is already running. Normally when a web server is closed that file is deleted, but in some cases, proper deletion doesn't happen so you have to remove the file manually
New Solutions
when you run rails s
=> Booting WEBrick
=> Rails 4.0.4 application starting in development on http://0.0.0.0:3000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
A server is already running. Check /your_project_path/tmp/pids/server.pid.
Exiting
So place your path shown here /your_project_path/tmp/pids/server.pid
and remove this server.pid file:
rm /your_project_path/tmp/pids/server.pid
OR Incase you're server was detached then follow below guidelines:
If you detached you rails server by using command "rails -d" then,
Remove rails detached server by using command
ps -aef | grep rails
OR by this command
sudo lsof -wni tcp:3000
then
kill -9 pID
OR use this command
To find and kill process by port name on which that program is running. For 3000 replace port on which your program is running.
sudo kill -9 $(lsof -i :3000 -t)
Old Solution:
rails s -p 4000 -P tmp/pids/server2.pid
Also you can find this post for more options
Rails Update to 3.2.11 breaks running multiple servers
Remove the file: C:/Sites/folder/Pids/Server.pids
Explanation
In UNIX land at least we usually track the process id (pid) in a file like server.pid. I think this is doing the same thing here. That file was probably left over from a crash.
lsof -wni tcp:3000
Then you should see the ruby process and you can run
kill -9 processid
you should be good to run the process now
rails s thin
running multiple processes doesn't seem like a good idea and from what i've read many people agree. I've noticed many memory leaks with rails unfortunately so I couldn't imagine having two processes running. I know with one overtime my page refreshes increasingly become slower because of the data being stored on memory.
kill -9 $(lsof -i tcp:3000 -t)
You can get rid of the process by killing it:
kill -9 $(lsof -i tcp:3000 -t)
$ lsof -wni tcp:3000
# Kill the running process
$ kill -9 5946
$ rm tmp/server.pids
foreman start etc start the service
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
It happens when you kill your server process and the pid file was not updated.
The best solution is to delete the file Server.pid.
Use the command
rm <path to file Server.pid>
On Windows Rails 5.2, delete this file
c:/Sites/<your_folder>/tmp/pids/server.pid
and run
rails s
again.
Just open that C:/Sites/folder/Pids/Server.pids and copy that 4 digit
value.that 4 digit value is nothing but a PID, which you need to kill
to stop the already running process.
then to stop the process use below command
kill -9 <pid>
once that already running process get stopped then hit
rails s to start the rails server
Probably you suspended the server by: ^Z.
The four digital number that vim C:/Sites/folder/Pids/Server.pidsoutputs is the process id.
You should kill -9 processid, replacing the process id with the 4 numbers that vim (or other editor) outputed.
Run:
in Ubuntu/linux
sudo rm /var/www/html/rails/WBPOCTEST/tmp/pids/server.pid
Or
pkill -9 ruby
or
lsof -wni tcp:3000
kill -9 pid
If you are on Windows, you just need to do only one step as 'rails restart' and then again type
'rails s'
You are good to go.
Run: fuser -k -n tcp 3000
This will kill the process running at the default port 3000.
On Ubuntu :-
sudo kill -9 $(lsof -i :3000 -t)
where,
3000 is port number
Works for me....
For older versions :-
rails s -p 3000 -P tmp/pids/server2.pid
I just had this issue and tried setting it to a different port, but the only thing I needed to do was to delete my [app_directory]/tmp/pids/server.pid and everything was good to go.
my docker container has no lsof. Try sudo kill -9 $(netstat -ano -p tcp | grep :3000 | awk '{ print $7 }' | grep -Po '^[\d]+')
I have face this issue in my rails application when i used docker in windows mechine.
i just removed the server.pid file from the project and restart the rails application.
You just need to delete C:/Sites/folder/Pids/Server.pid this file
Just go to the TMP folder in your application.
Go to the pids folder and delete the files present. Now rerun the server. This worked for me.
I was after a way I could intuitively understand (as opposed to just memorise).
All you need to know is two things
Run the ps command to list processes, and
Run the kill command to stop a process
Example
Then it's as simple as:
ps
# PID TTY TIME CMD
# 32953 ttys000 0:00.71 -zsh
# 33730 ttys001 0:00.67 -zsh
# 46844 ttys004 0:01.78 puma 4.3.12 (tcp://localhost:3000) [myapp]
look for the PID that matches your rails server, and pass it to kill
kill -9 46844
Run ps again to see that it's now stopped.
Note
For more info on -9, run man kill.

How to kill Rackup process in background?

I managed to host the RedMine using the Rackup and Puma by running the following code in the CMD.
rackup -I "script/rails" -s "puma" -O "-q" -E "production"
But this will keep the CMD still up and running. Thus, I created a windows service to run a .BAT file that will execute this command. It worked and the RedMine is now hosted in the background
And now my problems appears. I am now unable to stop the RedMine. Even if I stopped the service that run the .BAT file, the RedMine is still hosted. This is because I do not know how to kill the rackup process in the OnStop() function of the windows service.
Only way I could kill it is by killing the ruby.exe process. Hope you all could guide me to do this in a better way. Thanks

Unicorn init script - not starting at boot

I'm very new to system administration and have no idea how init.d works. So maybe I'm doing something wrong here.
I'm trying to start unicorn on boot, but somehow it just fails to start everytime. I'm able to manually do a start/stop/restart by simply service app_name start. Can't seem to understand why unicorn doesn't start at boot if manual starting stopping of service works. Some user permission issue maybe ??
My unicorn init script and the unicorn config files are available here https://gist.github.com/1956543
I'm setting up a development environment on Ubuntu 11.1 running inside a VM.
UPDATE - Could it be possible because of the VM ? I'm currently sharing the entire codebase (folder) with the VM, which also happens to contain the unicorn config needed to start unicorn.
Any help would be greatly appreciated !
Thanks
To get Unicorn to run when your system boots, you need to associate the init.d script with the default set of "runlevels", which are the modes that Ubuntu enters as it boots.
There are several different runlevels, but you probably just want the default set. To install Unicorn here, run:
sudo update-rc.d <your service name> defaults
For more information, check out the update-rc.d man page.
You can configure a cron job to start the unicorn server on reboot
crontab -e
and add
#reboot /bin/bash -l -c 'service unicorn_<your service name> start >> /<path to log file>/cron.log 2>&1'

Resources