[英]How to stop a Daemon Server in Rails?
我使用以下操作我的rails應用程序
$script/server -d webrick
在我的Ubuntu系統上,上面的命令在后台運行webrick服務器。 我可以使用kill命令殺死進程
$kill pid
rails是否提供任何命令來停止后台運行守護程序服務器?
就像rails提供的啟動服務器一樣,謝謝。
編輯什么時候適合啟動守護程序服務器? 任何實時場景都有助於謝謝
如果它有用,在linux上你可以找到哪個進程正在使用一個端口(在本例中為3000)你可以使用:
lsof -i:3000
它也將返回pid
像瑞安說:
你想要的pid在tmp / pids /
可能server.pid是你想要的文件。
你應該能夠運行kill -9 $(cat tmp/pids/server.pid)
來關閉一個守護進程的服務器。
耙子任務怎么樣?
desc 'stop rails'
task :stop do
pid_file = 'tmp/pids/server.pid'
pid = File.read(pid_file).to_i
Process.kill 9, pid
File.delete pid_file
end
用耙子停止或sudo耙子停止
守護程序服務器的進程ID存儲在應用程序目錄tmp / pids /中。 您可以將標准的kill process_id
與您在那里找到的信息一起使用。
殺死Ruby on Rails默認服務器(即WEBrick)的唯一正確方法是:
kill -INT $(cat tmp/pids/server.pid)
如果您正在運行Mongrel,這就足夠了:
kill $(cat tmp/pids/server.pid)
如果守護程序掛起,請使用kill -9
。 記住kill -9
的含義 - 如果Active Record高速緩存中保存的數據沒有刷新到磁盤,則會丟失數據。 (就像我最近做的那樣)
在您的終端中找出進程ID(PID):
$ lsof -wni tcp:3000
然后,使用PID列中的數字來終止進程:
$ kill -9 <PID>
pguardiario打敗了我,雖然他的實現有點危險,因為它使用SIGKILL
而不是(推薦) SIGINT
。 這是我傾向於導入到我的開發項目中的rake任務:
LIB /任務/ stopserver.rake
desc 'stop server'
task :stopserver do
pid_file = 'tmp/pids/server.pid'
if File.file?(pid_file)
print "Shutting down WEBrick\n"
pid = File.read(pid_file).to_i
Process.kill "INT", pid
end
File.file?(pid_file) && File.delete(pid_file)
end
當且僅當pidfile存在時,這會向服務器發出中斷。 如果服務器沒有運行,它不會拋出難看的錯誤,它會通知你它是否實際關閉了服務器。
如果您注意到服務器不想使用此任務關閉,請在Process.kill "INT"
行之后添加以下行,並嘗試升級到已修復此錯誤的內核。
Process.kill "CONT", pid
(帽子提示: jackr )
運行此命令:
locate tmp/pids/server.pid
輸出:此文件的完整路徑。 如果列表中顯示多個文件,請檢查項目目錄名稱以查找相關文件。
然后運行以下命令:
rm -rf [complete path of tmp/pids/server.pid file]
Ruby票證http://bugs.ruby-lang.org/issues/4777表明它是一個內核(Linux)錯誤。 他們提供了一個解決方法(基本上相當於Ctrl-C / Ctrl-Z),如果你妖魔化服務器就可以使用:
cat tmp/pids/server.pid
cat tmp/pids/server.pid
這似乎導致原始INT信號被處理,可能允許數據刷新等。
在這里我留下一個bash函數,如果你粘貼在.bashrc
或.zshrc
中,你會做一些事情,比如:
rails start # To start the server in development environment
rails start production # To start the server in production environment
rails stop # To stop the server
rails stop -9 # To stop the server sending -9 kill signal
rails restart # To restart the server in development environment
rails restart production # To restart the server in production environment
rails whatever # Will send the call to original rails command
這是功能:
function rails() {
if [ "$1" = "start" ]; then
if [ "$2" = "" ]; then
RENV="development"
else
RENV="$2"
fi
rails server -d -e "$RENV"
return 0
elif [ "$1" = "stop" ]; then
if [ -f tmp/pids/server.pid ]; then
kill $2 $(cat tmp/pids/server.pid)
return 0
else
echo "It seems there is no server running or you are not in a rails project root directory"
return 1
fi
elif [ "$1" = "restart" ]; then
rails stop && rails start $2
else
command rails $@
fi;
}
我寫的關於它的博客文章中有更多信息。
如果你使用-d,我不認為這樣做。 我只是殺了這個過程。
將來,只需打開另一個終端窗口並使用不帶-d的命令,它提供了一些非常有用的調試輸出。
如果這是生產,請使用乘客或瘦身,以便他們很容易停止進程或重新啟動服務器
one-liner: kill -INT `ps -e | grep ruby | awk '{print $1}'`
ps -e
列出系統上的每個進程
grep ruby
搜索ruby進程的輸出
awk
將該輸出的第一個參數(pid)傳遞給kill -INT
。
如果您只想查看PID,請嘗試使用echo而不是kill。
如果kill進程不起作用,則從MyRailsApp / tmp / pids /刪除文件server.pid
您可以通過在命令中添加-d
來在后台啟動服務器。 例如:
puma -d
要阻止它,只要殺死在端口3000上運行的任何進程:
kill $(cat tmp/pids/server.pid)
我來到這里是因為我試圖(不成功)停止正常的殺戮,並認為我做錯了什么。
kill -9是在rails服務器上停止ruby的唯一可靠方法嗎? 什么!? 你知道這個含義嗎? 可能是災難......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.