Is there a way to have limits for (let's say) apache2, that limits each forked child worker process to use max X memory, and not the entire process tree (apache2 and all of its forked children) to max X memory?

I have tried solutions with cgroups/systemd but everything limits the main process and all of its children combined, and not each child individually.

I am looking for a solution not only for apache2 but for all processes that spawn children.

  • As I try to explain, what you want to do is nearly impossible, and certainly unreasonable, if you want a systematic solution – Basile Starynkevitch Apr 18 at 13:49
  • 1
    Smells a big lot like some XY problem. What is the actual concern that you really have. Please edit your question to motivate it and give much more context. – Basile Starynkevitch Apr 18 at 13:52

Just improve the source code of your application (maybe Apache2 in your case, which is open source) to call setrlimit(2) after the successful call to fork(2), but before the call to execve(2).

Because you really want to have the setrlimit system call done after the fork (what you have then set remains till some further call to setrlimit, perhaps by the ulimit builtin of your shell).

Take some time to understand better how Linux syscalls(2) work (in particular fork & execve & setrlimit) and how and when you should use them. I recommend reading a good Linux system programming book, such as ALP.

Therefore, what you want to achieve is not realistic in general, unless you accept to change slightly every program you are using. The Unix philosophy which inspired the design of fork, execve , setrlimit forbids that.

Of course, you could in principle do insane LD_PRELOAD tricks to overload the behavior of fork (calling setrlimit after it returns 0) or of execve (calling setrlimit syscall before it) for dynamically linked executables (or equivalently, patch your own variant of libc.so), but that is not reasonable. See also file(1) or ldd(1) to detect such dynamically linked executables.

You could however use the ulimit shell builtin to put limits on Apache2 and all its children. And you might dive into Apache2 documentation, it has tons of settings regarding its child processes.

  • Although this may work for apache2 I don't think editing the source code of every process that spawns children is an achievable task. – Boyan Atanasov Apr 18 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.