Usman Ali
Usman Ali

Reputation: 1312

Rails: How to start rails server in background

When I run ruby script/server -e test, it runs on console. When I close the console, it also stops the process. I want to run the server in the background. How can I do this?

Upvotes: 17

Views: 33990

Answers (7)

Promise Preston
Promise Preston

Reputation: 28840

To add more context to Kryptman's answer and oddmeter's comment.

I had this concern when trying to start a Puma rails server in background mode.

To achieve this using the -d flag, just run the command below:

rails server puma -d

If you want to specify a different port for the operation use the command below with the -p flag:

rails server -p 3002 puma -d

If you want to check the puma running server process just run the command below:

ps aux | grep puma

If you want to stop the running process, run the command below.

ps aux | grep puma | awk {'print $2'} | xargs kill

OR the command below if you want to stop the process immediately. However, this is not recommended:

ps aux | grep puma | awk {'print $2'} | xargs kill -9

Note: awk selects the second item ($2) in the row output which is the PID, while xargs accepts the PID from awk as an argument.

You can also achieve this with systemd - Puma Systemd Configuration for Rails Not Working.

Upvotes: 0

mvndaai
mvndaai

Reputation: 3831

One way to do this, which even stays connected on ssh is use Screen, which makes a sub terminal that isn't affected by your current console. sudo apt-get install screen Open screen screen Then start rails rails server &. The & just makes it run it the background. To stop it type kill -9 # where # is the number it gives you when you start it.

Press 'Crtl + A' to escape and type screen -r to get back in to the screen terminal.

Upvotes: 2

Ankit Singh
Ankit Singh

Reputation: 2622

Its a bit late to answer. But it would be good for future person.

The easiest and quicker way to put rails (or any service in background) assuming it to have Unix/Linux OS

$ nohup rails server &

This can be used for any service like this

$ nohup <service command> &

Upvotes: 16

Kryptman
Kryptman

Reputation: 1016

If you are using thin:

rails server thin -d

And, in order to stop it:

kill -9 $(cat tmp/pids/server.pid)

Upvotes: 25

jonnii
jonnii

Reputation: 28312

The other option is to use apache with passenger, it's really easy to set up and once you've done it once you can use it for all your other apps. Plus it's most likely going to be near to what you're running on production, so that's another benefit.

If you're on a mac you can also get the passenger preference pane which simplifies the apache configuration steps.

Upvotes: 0

vise
vise

Reputation: 13383

You can run it as a daemon with script/server -d

Upvotes: 26

tjeden
tjeden

Reputation: 2079

Run your server with & at the end:

script/server -e test&

It will put it to background.

Or you can use other server like thin: http://code.macournoyer.com/thin/

(sudo) gem install thin

And then start and stop it using

thin start
thin stop

Upvotes: 4

Related Questions