2010-08-01 114 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