I've had problems running Mac OS X El Capitan 10.11.6 that seemed to be related to resource limitation. I saw things like
error: cannot spawn or
unable to fork or
-bash: fork: retry: Resource temporarily unavailable -bash: fork: Resource temporarily unavailable
Because my system was not completely freezing or spreading errors everywhere, I expected what was happening was that I had reached the limit number of processes that I could perform.
$ ulimit -u 709
709 is a lot of process, but I ran a lot of servers on my Mac, so it seemed possible to use as many. I have tried this command, which displays the number of processes performed by the user (actually, 1 more than the number of processes)
$ ps -xu $(id -ru) | wc -l fork: Resource temporarily unavailable
OK, I could not execute this command, which creates 3 processes, so I was probably at the limit of the process. And the error message does not start with
-bash so it was not about
ulimit Is. I was hitting the system limit
I was able to confirm this by running a shell like
$ id -ru 501 $ sudo -i root# ps -xu 501 | wc -l 706
Yes, 706 <709, but
mdworker the processes come and go routinely, so it was obvious enough to get closer to the limit.
How to increase the limit of the number of processes that I can execute? I looked for answers and found articles suggesting to create a file
/Library/LaunchDaemons/limit.maxproc.plist with this content:
Label limit.maxproc ProgramArguments launchctl limit maxproc 2048 4096 RunAtLoad ServiceIPC
I did it and restarted and now
$ ulimit -u 2048
Which would be great except
$ sysctl kern.maxproc kern.maxproc: 1064
THIS IS DANGEROUS. This means that the entire system is always limited to a maximum of 1064 processes, but that an individual user is allowed to create more than that. When the system reaches the maximum number of processes, it usually hangs fast and very badly. It is therefore very important that no user is allowed to create enough processes to fill the process table. That's why, on the Mac, users are limited to 2/3 of the total number of processes.
I found similar suggestions regarding the maximum number of open files
maxfilesperproc, which are even more dangerous, because a single process should absolutely not be allowed to open the limit of the total number of open files at the system level.
The other problem is that if 709 processes are not enough, it is unlikely that 1000 processes are too.
So, what is the good way to fix "Too many open files" or "fork: resource temporarily unavailable" on OS X El Capitan?