使用 Upstart 设置内存消耗限制
我最近变得非常喜欢Upstart。之前我一直在使用 God、Monit 和 Bluepill,但我不太喜欢这些解决方案,所以我尝试一下 Upstart。
我一直在使用 Foreman gem 在 /etc/init
中为我的进程生成一些基本的 Upstart 配置文件。但是,这些生成的文件仅处理崩溃进程的重生。我想知道是否可以告诉 Upstart 重新启动一个正在消耗的进程,例如 > 150mb
内存,就像您使用 Monit、God 或 Bluepill 一样。
我通读了 Upstart 文档 和 这 看起来就是我正在寻找的东西。虽然我不知道如何配置这样的东西。
我基本上想要的很简单。如果内存使用量 >,我想重新启动我的
web
进程。 150mb 内存。这些是我拥有的文件:
|-- myapp-web-1.conf
|-- myapp-web-2.conf
|-- myapp-web-3.conf
|-- myapp-web.conf
|-- myapp.conf
它们的内容是:
myapp.conf
pre-start script
bash << "EOF"
mkdir -p /var/log/myapp
chown -R deployer /var/log/myapp
EOF
end script
myapp-web.conf
start on starting myapp
stop on stopping myapp
myapp-web-1.conf / < strong>myapp-web-2.conf / myapp-web-3.conf
start on starting myapp-web
stop on stopping myapp-web
respawn
exec su - deployer -c 'cd /var/applications/releases/20110607140607; cd myapp && bundle exec unicorn -p $PORT >> /var/log/myapp/web-1.log 2>&1'
非常感谢任何帮助!
I've recently become quite fond of Upstart. Previously I've been using God, Monit and Bluepill but I don't really like these solutions so I'm giving Upstart a try.
I've been using the Foreman gem to generate some basic Upstart configuration files for my processes in /etc/init
. However, these generated files only handle the respawning of a crashed process. I was wondering whether it's possible to tell Upstart to restart a process that's consuming for example > 150mb
of memory, as you would with Monit, God or Bluepill.
I read through the Upstart docs and this looks like the thing I'm looking for. Though I have no clue how to config something like this.
What I basically want is quite simple. I want to restart my web
process if the memory usage is > 150mb
ram. These are the files I have:
|-- myapp-web-1.conf
|-- myapp-web-2.conf
|-- myapp-web-3.conf
|-- myapp-web.conf
|-- myapp.conf
And their contents are:
myapp.conf
pre-start script
bash << "EOF"
mkdir -p /var/log/myapp
chown -R deployer /var/log/myapp
EOF
end script
myapp-web.conf
start on starting myapp
stop on stopping myapp
myapp-web-1.conf / myapp-web-2.conf / myapp-web-3.conf
start on starting myapp-web
stop on stopping myapp-web
respawn
exec su - deployer -c 'cd /var/applications/releases/20110607140607; cd myapp && bundle exec unicorn -p $PORT >> /var/log/myapp/web-1.log 2>&1'
Any help much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此附加到
myapp-web-*.conf
末尾将导致任何尝试分配超过150mb
内存的分配调用返回ENOMEM
:进程此时可能会崩溃,也可能不会。这要看流程了!
这里是在 Upstart Source 中对此进行了测试。
Appending this to the end of
myapp-web-*.conf
will cause any allocation calls trying to allocate more than150mb
of memory to returnENOMEM
:The process might crash at this point, or it might not. That's up to the process!
Here's a test for this in the Upstart Source.
从 Upstart 文档来看,限制来自于 rlimit 系统调用选项。 (http://upstart.ubuntu.com/cookbook/#limit)
自 Linux 2.4+ 起设置 rss(驻留集大小)无效。
其他答案中已经建议的替代方案是 as ,它设置虚拟内存地址空间大小限制。这将对设置“真实”内存限制产生非常不同的效果。
setrlimit 手册页摘录:
From the Upstart docs, the limits come from the
rlimit
system call options. (http://upstart.ubuntu.com/cookbook/#limit)Since Linux 2.4+ setting the rss (Resident Set Size) has no effect.
An alternative already suggested in other answers is as which sets the virtual memory Address Space size limits. This will have a very different effect of setting 'real' memory limits.
Excerpt from man pages for setrlimit: