Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I launched a server application and I want to run it for a long time for testing purpose. Unfortunately, I forgot to set before ulimit -c unlimited to catch an eventual crash and inspect it. Is there something I can do?

share|improve this question
up vote 25 down vote accepted

On recent versions of Linux (since 2.6.36), you can use the prlimit command and system call to set resource limits on an arbitrary process (given appropriate permissions):

$ prlimit --core=unlimited: --pid $$
$ prlimit --core --pid $$
CORE     max core file size unlimited unlimited blocks

You need util-linux-2.21 for the prlimit command, but you should be able to throw together a quick program to invoke the prlimit system call otherwise:

int prlimit(pid_t pid, int resource, const struct rlimit *new_limit, struct rlimit *old_limit);

If you don't have a new enough version of Linux (or another OS) the only fix I'm aware of is to connect to the process with gdb and issue setrlimit from the debugger:

$ gdb -p $PID
(gdb) set $rlim = &{0ll, 0ll}
(gdb) print getrlimit(9, $rlim)
$1 = 0
(gdb) print *$rlim
$2 = {-1, -1}
(gdb) set *$rlim[0] = 1024*1024
(gdb) print setrlimit(9, $rlim)
$3 = 0

This is for setting ulimit -m, RLIMIT_AS = 9; exactly the same applies for ulimit -c (RLIMIT_CORE, numeric value 4 on Linux on x86-64). For "unlimited", use RLIM_INFINITY, usually -1. You should check in /usr/include/bits/types.h what the size of rlim_t is; I'm assuming long long (it's actually unsigned, but using a signed type makes "unlimited" -1 easier to read).

share|improve this answer
+1 The gdb technique is really cool. One caveat, it seems you can't increase the number of open files for a non-root process past its hard limit, the setrlimit call returns -1, and errno is 22 (invalid argument). – Steve Kehlet Jul 9 '14 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .