在 Rails 进程不再有用后终止它
我正在使用 nginx 运行 Rails 应用程序(确切地说是 Tracks)。启动的 Rails 进程似乎会无限期地持续下去?是不是该停下来了?
我的共享主机上的 RAM 分配较低,并且希望能够在 10 分钟后终止 Rails 进程。有没有办法在 nginx 或 Passenger 中做到这一点?
与此同时,我每 10 分钟就使用 cron 运行一次 bash 脚本:
PID=$(ps ax|grep [R]ails.*lytracks | cut -f2 -d" " | head -n1)
if [ $PID ]; then
kill -SIGUSR1 $PID
else
echo Not running
fi
I'm running a Rails app (Tracks, to be exact) with nginx. The Rails process that starts seems to persist indefinitely? Is it suppose to stop?
I have a low RAM allotment on my Shared Hosting and want to be able to kill the Rails process after, say, 10 minutes. Is there a way to do this in nginx or Passenger?
In the meantime, I'm running this bash script with cron every 10 minutes:
PID=$(ps ax|grep [R]ails.*lytracks | cut -f2 -d" " | head -n1)
if [ $PID ]; then
kill -SIGUSR1 $PID
else
echo Not running
fi
你可以这样做,但你不应该这样做。
Rails(在生产模式下)通常不会泄漏内存,因此重新启动进程应该不会有任何影响。
一个具有合理负载的健康 Rails 应用程序应该稳定在大约 30-70MB RAM 并永远保持在那里。
每 10 分钟重新启动一次意味着每 10 分钟一些用户就会看到一个需要 20 秒才能加载的页面。或者根本无法加载。
您尝试像使用 CGI PHP 脚本一样使用 Rails。它不是故意这样做的。
如果出现内存泄漏,您应该尝试找出导致内存泄漏的原因,然后修复它。
You can do that, but you shouldn't.
Rails(in production mode) does not normally leak memory, so restarting the process should have no effect.
A healthy rails app with reasonable load should stabilize at about 30-70MB RAM and stay there forever.
Restarting it every 10 minutes means that every 10 minutes some of your users will see a page that takes 20 seconds to load. Or fail to load at all.
You're trying to use Rails like you would use a CGI PHP script. It's not meant to do that.
If you have memory leaks, you should try and find out what's causing them, then fix it.