37

I have a long running process that is eventually going to hit the max open file limit. I know how to change that after it fails, but is there a way to change that for the running process, from the command line?

mormegil
  • 125
  • 7
kāgii
  • 471
  • 1
  • 4
  • 4

5 Answers5

38

As documented here, the prlimit command, introduced with util-linux 2.21 allows you to read and change the limits of running processes.

This is a followup to the writable /proc/<pid>/limits, which was not integrated in mainline kernel. This solution should work.

If you don't have prlimit(1) yet, you can find the code to a minimalistic version in the prlimit(2) manpage.

Totor
  • 2,876
  • 3
  • 22
  • 31
Sig-IO
  • 1,046
  • 9
  • 11
  • 2
    +1 for the minimalistic version in the manpage ! – Totor Dec 14 '17 at 10:39
  • Warning: "memcached" will segfault if you change "nolimit" on it's process after it encountered a limit. Not always a good idea to use the writeable limits – John Feb 09 '19 at 01:31
  • OpenJDK 11 java is also segfaulted (but I only tried once). – Hontvári Levente Mar 24 '20 at 11:41
  • Kernel doesn't provide interface for a process to be informed about changed limits. Some programs query limits during the process startup and may crash or misbehave if the limits are changed during the runtime. Increasing the limits at runtime should be okay for all correctly written programs, though. – Mikko Rantalainen Aug 23 '21 at 08:34
33

On newer kernels (2.6.32+) on CentOS/RHEL you can change this at runtime with /proc/<pid>/limits:

cd /proc/7671/
[root@host 7671]# cat limits  | grep nice
Max nice priority         0                    0                    
[root@host 7671]# echo -n "Max nice priority=5:6" > limits
[root@host 7671]# cat limits  | grep nice
Max nice priority         5                    6                    
wfaulk
  • 6,828
  • 7
  • 45
  • 75
Sig-IO
  • 1,046
  • 9
  • 11
  • That rules! Had no idea you could write to the limits file. – clee May 15 '12 at 22:11
  • 4
    doesn't work for me on ubuntu 12.04 – Poma Jul 10 '12 at 07:06
  • 7
    *This does not work on my 3.2 kernel*. I guess your distribution has a specific unofficial patch for this because I see no trace of [this patch](https://lkml.org/lkml/2009/9/28/211) in the kernel's [fs/proc/base.c](http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/proc/base.c?id=v3.8). – Totor Apr 19 '13 at 13:43
  • Could be... I know it's present in RHEL / CentOS kernels. It doesn't seem to be present in Debian kernels. – Sig-IO Nov 12 '15 at 10:38
  • 3
    'setprlimit' is the future-proof and recommended way, see the next answer – Sig-IO Nov 12 '15 at 10:51
  • Modern Linux kernel provides `prlimit()` for setting the limits at runtime. Writing to `/proc//limits` was a suggested patch that never got traction in official kernel. – Mikko Rantalainen Aug 23 '21 at 08:36
4

On newer version of util-linux-ng you can use prlimit command, for more infomation read this link https://superuser.com/questions/404239/setting-ulimit-on-a-running-process

c4f4t0r
  • 5,149
  • 3
  • 28
  • 41
2

You can try ulimit man ulimit with the -n option however the mag page does not most OS's do not allow this to be set.

You can set a system wide file descriptions limit using sysctl -w fs.file-max=N and make the changes persist post boot up in /etc/sysctl.conf

However I would also suggest looking at the process to see if it really needs to have so many files open at a given time, and if you can in fact close some files down and be more efficient in the process.

Oneiroi
  • 2,008
  • 1
  • 15
  • 28
0

The process could change its own soft limits if programmed to do so (or if you manage to hack it), but it can't raise its hard limits unless it has the CAP_SYS_RESOURCE capability. You can inspect the limits at runtime in /proc/$pid/limits .

Tobu
  • 4,367
  • 1
  • 23
  • 31