【问题标题】:Running Webrick server in background?在后台运行 Webrick 服务器?
【发布时间】:2010-08-01 10:00:59
【问题描述】:
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

在这个命令之后,我必须保持终端打开,甚至无法使用 Cmd+z 退出。我不能将它作为后台服务运行吗?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby webrick


    【解决方案1】:

    输出已经给你答案了:

    => Call with -d to detach
    

    【讨论】:

    • 如何停止分离的 Rails 服务器?
    • @user3383458,你可以用pkill -f rails杀死。 (“-f”并不意味着“强制”,而是“搜索完整命令行而不是仅仅搜索进程名称”。所以它们会优雅地关闭。)如果你不想去狂暴并杀死所有Rails实例,您可以使用ps -ef | grep rails找到所有实例并使用kill [pid]杀死您想要的。
    【解决方案2】:

    一般来说,你可以使用:

    command &
    

    它会从终端窗口中分离出来。

    如果您使用的是 Linux,另一种选择是使用screen

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

    瞧!它是分离的。然后你可以打电话给screen -r,你的进程会像什么都没发生一样回来。

    【讨论】:

    • 我认为 command & 只会在后台运行它。当您关闭终端时,它仍然会停止。但是你的屏幕建议很好。
    • nohup rails server & 将分离并在后台运行,即使终端关闭或用户注销。
    • @fijiaaron 你能告诉我以后如何停止它吗?
    【解决方案3】:

    如果你运行rails s --help 你会看到一堆选项

    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.
    

    您需要的是将其作为守护程序运行。因此,解决方案是: rails s -d

    【讨论】:

      【解决方案4】:

      杂种宝石可以轻松做到这一点。

      gem install mongrel
      

      那你应该可以用了

      mongrel_rails start -d
      

      -d 用于守护程序模式。

      【讨论】:

      • 问题是,我无法安装或卸载 Mongrel,两者都出错。
      猜你喜欢
      • 2017-01-28
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多