'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?



Solution 1:[1]

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

Solution 2:[2]

If you are using thin:

rails server thin -d

And, in order to stop it:

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

Solution 3:[3]

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> &

Solution 4:[4]

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

Solution 5:[5]

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.

Solution 6:[6]

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.

Solution 7:[7]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vise
Solution 2 Kryptman
Solution 3 iankits
Solution 4 tjeden
Solution 5
Solution 6 jonnii
Solution 7 halfer