Set `ulimit -c` from outer shell
I have a program that automatically starts at boot and calls coredump sporadically.
I want to write the output, but I cannot set ulimit -c
programmatically (it defaults to 0 and flushes every time).
I've tried using a bash script as well as python sh
, os.system
and subprocess
but I can't seem to get it to work.
source to share
The process can only set resource limits for itself and its children. He cannot set resource limits for his ancestor. By calling os.system('ulimit -c')
, you are asking the "ulimit" child process to set the resource limit of the parent "Python" process.
Your Python program can set a resource limit using a module resource
:
import resource
resource.setrlimit(
resource.RLIMIT_CORE,
(resource.RLIM_INFINITY, resource.RLIM_INFINITY))
source to share
I'm guessing your problem is that you don't understand what is rlimit
set for each process. If you use os.system
in Python to call ulimit, this will only set the ulimit in that newly spawned shell process, which will exit immediately, after which nothing has changed.
Instead, you need to run ulimit
in a shell that launches your program. The process your program is running in then inherits this rlimit from the shell.
I don't think there is any way to change the rlimit of process X from process Y where X! = Y.
EDIT : I will need to retrace the last answer, at least in case you are on Linux. There is a Linux-specific syscall prlimit
that allows you to change the rlimits of another process, and it also appears to be available in a Python module resource
, although it is not documented there. See Manpage prlimit(2)
; I would assume that the function available in Python uses the same arguments.
source to share
To add another solution to the mix - I globally set 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 is also possible with a command os.system
in python.
source to share