2010-08-01 116 views
13
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 

此命令后,我必须保持终端打开,甚至不能使用命令+ z。我不能将它作为后台服务运行吗?在后台运行Webrick服务器?

感谢

回答

36

输出已经给你答案:

=> Call with -d to detach 
+4

我将如何停止分离的Rails服务器? – user3383458 2014-04-25 13:19:45

+2

@ user3383458,你可以用'pkill -f rails'杀死。 (“-f”并不意味着“强制”,而是“搜索**完整的**命令行而不仅仅是进程名称”,所以他们会优雅地关闭。)如果你不想疯狂杀人Rails的所有实例,你可以用'ps -ef |找到它们grep rails'并用'kill [pid]'杀死你想要的。 – AlicanC 2015-04-30 18:45:43

1

的杂种宝石可以做到这一点很容易。

gem install mongrel 

那么你应该能够使用

mongrel_rails start -d 

-d进行守护模式。

+0

问题是,我无法安装或卸载Mongrel,两者都出错。 – 3zzy 2010-08-01 10:22:18

6

在一般情况下,你可以使用:

command & 

,将它从终端窗口分离。

如果您使用的是Linux,另一个选项是使用screen

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

瞧!它是分离的。然后你可以拨打screen -r,你的程序会回来,好像什么都没有发生。

+0

我觉得命令&只会在后台运行它。关闭终端时它仍会停止。但你的屏幕建议是好的。 – Nick 2010-08-01 10:18:02

+0

@Nick它不在Ubuntu上 – quantumSoup 2010-08-01 10:19:19

+1

'nohup rails server&'将在后台分离并运行,即使终端关闭或用户注销。 – fijiaaron 2012-10-20 20:28:26

2

如果运行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