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 -c
programmatically (It's defaulted to 0, and resets every time).
I've tried using a bash script, as well as python's sh
, os.system
and subprocess
, but I can't get it to work.
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.
Your Python program can set its resource limit using the resource
module:
import resource
resource.setrlimit(
resource.RLIMIT_CORE,
(resource.RLIM_INFINITY, resource.RLIM_INFINITY))
I'm guessing your problem is that you haven't understood that rlimit
s are set per process. If you use os.system
in 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.
What you need to do, instead, is to run ulimit
in the shell that starts your program. The process your program is running in will then inherit that rlimit from the shell.
I do not think there is any way to alter the rlimit of process X from process Y, where 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 prlimit
that allows you to change the rlimits of a different process, and it also does appear to be available in Python's resource
module, though it is undocumented there. See the manpage prlimit(2)
instead; I'd assume that the function available in Python uses the same arguments.
To throw another solution into the mix - I globally set the ulimit in debian using limits.conf:
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.system
command in python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With