Ruby on rails 如何在Rails中停止守护进程服务器?
我使用以下命令运行rails应用程序Ruby on rails 如何在Rails中停止守护进程服务器?,ruby-on-rails,ubuntu,Ruby On Rails,Ubuntu,我使用以下命令运行rails应用程序 $script/server -d webrick 在我的Ubuntu系统上,上面的命令在后台运行webrick服务器。我可以使用kill命令终止进程 $kill pid rails是否提供任何命令来停止后台运行的守护进程服务器 就像rails提供的启动服务器的方法一样,谢谢 何时适合启动守护程序服务器?任何实时场景都会有帮助,谢谢我不认为如果使用-d。我会扼杀这个过程 将来,只需打开另一个终端窗口,并使用不带-d的命令,它将提供一些真正有用的
$script/server -d webrick
在我的Ubuntu系统上,上面的命令在后台运行webrick服务器。我可以使用kill命令终止进程
$kill pid
rails是否提供任何命令来停止后台运行的守护进程服务器
就像rails提供的启动服务器的方法一样,谢谢
何时适合启动守护程序服务器?任何实时场景都会有帮助,谢谢我不认为如果使用-d。我会扼杀这个过程 将来,只需打开另一个终端窗口,并使用不带-d的命令,它将提供一些真正有用的调试输出
如果这是生产环境,请使用诸如passenger或thin之类的工具,以便它们能够轻松停止进程或重新启动服务器守护进程服务器的进程id存储在应用程序目录tmp/pids/中。您可以使用标准的
kill进程\u id
和您在那里找到的信息。就像Ryan说的:
您想要的pid在tmp/pid中/
可能server.pid就是您想要的文件
您应该能够运行
kill-9$(cat tmp/pids/server.pid)
关闭守护服务器。如果有用,在linux上,您可以找到哪个进程正在使用端口(在本例中为3000),您可以使用:
lsof-i:3000
它也会返回pid我来这里是因为我试图(失败地)停止正常的杀戮,并认为我做错了什么 kill-9是阻止RubyonRails服务器的唯一可靠方法吗?
什么!?你知道这意味着什么吗?可能是一场灾难 杀死Ruby on Rails默认服务器(即WEBrick)的唯一正确方法是: 如果您正在运行Mongrel,这就足够了:
kill $(cat tmp/pids/server.pid)
如果守护进程挂起,请使用kill-9
。请记住kill-9
-如果活动记录缓存中保存的数据未刷新到磁盘,您将丢失数据。(就像我最近做的那样)一个rake任务怎么样
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
使用rake-stop或sudo-rake-stop运行Ruby票据,表明这是内核(Linux)缺陷。它们提供了一种变通方法(基本上相当于Ctrl-C/Ctrl-Z方法),如果您已经将服务器妖魔化,可以使用:
cat tmp/pids/server.pid
cat tmp/pids/server.pid
SIGKILL
而不是(推荐的)SIGINT
。以下是我倾向于导入到我的开发项目中的rake任务:
lib/tasks/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
(帽子提示:)在终端中查找进程id(PID): 然后,使用PID列中的数字终止进程:
$ kill -9 <PID>
$kill-9
一行: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这里我留下了一个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;
}
我在文章中写了更多关于它的信息。运行此命令:
locate tmp/pids/server.pid
输出:
此文件的完整路径。若列表中显示多个文件,请检查项目目录名以查找相关文件
然后运行以下命令:
rm -rf [complete path of tmp/pids/server.pid file]
通过向命令中添加
-d
,可以在后台启动服务器。例如:
puma -d
要停止它,只需杀死端口3000上正在运行的任何进程:
kill $(cat tmp/pids/server.pid)
即使在Mac/unix/bash上。。。是的。这非常有用
lsof-i:3000
告诉我运行ruby的PIDkill$(lsof-i:3000-t)
-t选项代表简洁,这意味着它将只输出进程ID;下面是我的bash_别名alias stopRails='kill-9$(lsof-i:3000-t)
和alias startRails='rails server-d'
(如果您不在应用程序目录中,将失败,但这没关系)以及最后的alias restartRails='stopRails&&startRails'
@Sudhi,你的解决方案完全是一个令人敬畏的RubyFied版本:pid=`lsof-i:3000-t`.chomp.to_i
(@gery在-t
标志上的调用非常好,我今天学到了一些新东西!)在lsof-i:3000
我得到了ruby的pid,然后kill-9 1406或whateverthepidofrubywas
-easypeasy-谢谢!是的,并且会丢失内存中但尚未在磁盘上的数据。这里有一行代码,可以分配给~/.bashrc文件中的别名:kill-9$(lsof-i:3000)和>/dev/null
。&>
之后的部分是可选的——它只是抑制kill命令的一些输出。我建议不要运行该命令,因为浏览器可能打开了端口3000,因为它正在与Rails服务器通信。我曾多次以这种方式意外杀死Chrome.:)我只是因为杀戮9而丢失了数据。数据尚未刷新到SQLite,只是在内存中。有趣的是,它已经在记忆中一周了。这是一个评论,不是对这个问题的回答…
rm -rf [complete path of tmp/pids/server.pid file]
puma -d
kill $(cat tmp/pids/server.pid)