14
MBPro:shovell myname$ ruby script/server
=> Booting WEBrick
=> Rails 2.3.8 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-08-01 15:28:35] INFO  WEBrick 1.3.1
[2010-08-01 15:28:35] INFO  ruby 1.9.1 (2010-07-02) [i386-darwin10.4.0]
[2010-08-01 15:28:35] INFO  WEBrick::HTTPServer#start: pid=36349 port=3000

After this command, I have to keep the terminal open, can't even get out using Cmd+z. Can't I run it as a background service?

Thanks

eozzy
  • 66,048
  • 104
  • 272
  • 428

4 Answers4

38

The Output is already giving you the answer:

=> Call with -d to detach
jigfox
  • 18,057
  • 3
  • 60
  • 73
  • 4
    How would I stop the detached Rails server? – Saggex Apr 25 '14 at 13:19
  • 2
    @user3383458, you can kill with `pkill -f rails`. ("-f" does not mean "force", but "search the **full** command line instead of just process name". So they will be shutting down gracefully.) If you don't want to go berserk and kill all instances of Rails, you can find all of them with `ps -ef | grep rails` and kill the ones you want with `kill [pid]`. – AlicanC Apr 30 '15 at 18:45
6

In general, you can use:

command &

And it will detach from the terminal window.

If you are using Linux, another options is to use screen:

screen
# start your process
# press Ctrl+a
# press Ctrl+d

Voila! It's detached. Then you can call screen -r and your process will be back as if nothing happened.

quantumSoup
  • 27,197
  • 9
  • 43
  • 57
  • I think command & will only run it in the background. It will still stop when you close the terminal. But your screen suggestion is good. – Nick Aug 01 '10 at 10:18
  • 1
    `nohup rails server &` will detach and run in the background, even if the terminal closes or the user logs off. – fijiaaron Oct 20 '12 at 20:28
  • @fijiaaron Can you please give me a hint on how to stop it later? – nomann Dec 07 '14 at 09:01
2

If you run rails s --help You will see a bunch of options

Usage: rails server [mongrel, thin etc] [options]
    -p, --port=port                  Runs Rails on the specified port.
                                     Default: 3000
    -b, --binding=IP                 Binds Rails to the specified IP.
                                     Default: localhost
    -c, --config=file                Uses a custom rackup configuration.
    -d, --daemon                     Runs server as a Daemon.
    -u, --debugger                   Enables the debugger.
    -e, --environment=name           Specifies the environment to run this server under (test/development/production).
                                     Default: development
    -P, --pid=pid                    Specifies the PID file.
                                     Default: tmp/pids/server.pid

    -h, --help                       Shows this help message.

The one that you need is to run it as a Daemon. Hence, the solution is: rails s -d

Chirag
  • 967
  • 8
  • 15
1

The mongrel gem can do this easy.

gem install mongrel

Then you should be able to use

mongrel_rails start -d

-d for daemon mode.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337