bash 从外部设置`ulimit -c`

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25475906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:13:31  来源:igfitidea点击:

set `ulimit -c` from outside shell

pythonbashshellulimit

提问by Jono

I have a program that's running automatically on boot, and sporadically causing a coredump.

我有一个在启动时自动运行的程序,偶尔会导致核心转储。

I'd like to record the output, but I can't seem to set ulimit -cprogrammatically (It's defaulted to 0, and resets every time).

我想记录输出,但我似乎无法以ulimit -c编程方式设置(默认为 0,并且每次都重置)。

I've tried using a bash script, as well as python's sh, os.systemand subprocess, but I can't get it to work.

我试过使用 bash 脚本以及 python 的sh, os.systemand subprocess,但我无法让它工作。

回答by Rob?

A process may only set resource limits for itself and its children. It cannot set resource limits for its ancestor. By calling os.system('ulimit -c'), you are asking the child "ulimit" process to set the resource limit of the ancestor "Python" process.

一个进程只能为它自己和它的子进程设置资源限制。它不能为其祖先设置资源限制。通过调用os.system('ulimit -c'),您要求子“ulimit”进程设置祖先“Python”进程的资源限制。

Your Python program can set its resource limit using the resourcemodule:

您的 Python 程序可以使用以下resource模块设置其资源限制:

import resource

resource.setrlimit(
    resource.RLIMIT_CORE,
    (resource.RLIM_INFINITY, resource.RLIM_INFINITY))

回答by Jono

To throw another solution into the mix - I globally set the ulimit in debian using limits.conf:

为了混合使用另一个解决方案 - 我使用limits.conf在debian中全局设置ulimit:

grep -q -F '* soft core 100000' /etc/security/limits.conf || echo '* soft core 100000' >> /etc/security/limits.conf
grep -q -F 'root hard core 100000' /etc/security/limits.conf || echo 'root hard core 100000' >> /etc/security/limits.conf

This was also possible using the os.systemcommand in python.

这也可以使用os.systempython 中的命令。

回答by Dolda2000

I'm guessing your problem is that you haven't understood that rlimits are set per process. If you use os.systemin Python to call ulimit, that is only going to set the ulimit in that newly spawned shell process, which then immediately exits after which nothing has been changed.

我猜你的问题是你不明白rlimit每个进程都设置了s 。如果您os.system在 Python 中使用调用 ulimit,那只会在新生成的 shell 进程中设置 ulimit,然后立即退出,之后没有任何更改。

What you need to do, instead, is to run ulimitin the shell that starts your program. The process your program is running in will then inherit that rlimit from the shell.

相反,您需要做的是ulimit在启动程序的 shell 中运行。您的程序正在运行的进程将从 shell 继承该 rlimit。

I do not think there is any way to alter the rlimit of process X from process Y, where X != Y.

我认为没有任何方法可以改变进程 Y 的进程 X 的 rlimit,其中 X != Y。

EDIT: I'll have to take that last back, at least in case you're running in Linux. There is a Linux-specific syscall prlimitthat allows you to change the rlimits of a different process, and it also does appear to be available in Python's resourcemodule, though it is undocumented there. See the manpage prlimit(2)instead; I'd assume that the function available in Python uses the same arguments.

编辑:我必须把最后一个拿回来,至少在你在 Linux 上运行的情况下。有一个特定于 Linux 的系统调用prlimit,它允许您更改不同进程的 rlimits,并且它似乎也在 Python 的resource模块中可用,尽管它在那里没有记录。请参阅联机帮助页prlimit(2);我假设 Python 中可用的函数使用相同的参数。