使用 Upstart 设置内存消耗限制

发布于 2024-11-14 19:59:42 字数 1395 浏览 2 评论 0原文

我最近变得非常喜欢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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2024-11-21 19:59:43

将此附加到 myapp-web-*.conf 末尾将导致任何尝试分配超过 150mb 内存的分配调用返回 ENOMEM

limit rss 157286400 157286400

进程此时可能会崩溃,也可能不会。这要看流程了!

这里是在 Upstart Source 中对此进行了测试

Appending this to the end of myapp-web-*.conf will cause any allocation calls trying to allocate more than 150mb of memory to return ENOMEM:

limit rss 157286400 157286400

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.

罪#恶を代价 2024-11-21 19:59:43

从 Upstart 文档来看,限制来自于 rlimit 系统调用选项。 (http://upstart.ubuntu.com/cookbook/#limit)

自 Linux 2.4+ 起设置 rss(驻留集大小)无效。

其他答案中已经建议的替代方案是 as ,它设置虚拟内存地址空间大小限制。这将对设置“真实”内存限制产生非常不同的效果。

limit as <soft limit> <hard limit>

setrlimit 手册页摘录:

RLIMIT_AS
进程虚拟内存(地址空间)的最大大小(以字节为单位)。此限制会影响对 brk(2)、mmap(2) 和 mremap(2) 的调用,
超过此限制后会失败并出现错误 ENOMEM。自动堆栈扩展也会失败(并生成一个 SIGSEGV
如果没有通过 sigaltstack(2)) 提供可用的备用堆栈,则终止该进程。由于该值很长,因此在具有
32 位长,要么此限制最多为 2 GiB,要么此资源不受限制。

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.

limit as <soft limit> <hard limit>

Excerpt from man pages for setrlimit:

RLIMIT_AS
The maximum size of the process's virtual memory (address space) in bytes. This limit affects calls to brk(2), mmap(2), and mremap(2),
which fail with the error ENOMEM upon exceeding this limit. Also automatic stack expansion will fail (and generate a SIGSEGV that
kills the process if no alternate stack has been made available via sigaltstack(2)). Since the value is a long, on machines with a
32-bit long either this limit is at most 2 GiB, or this resource is unlimited.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文