Skip to contentSkip to conversation

Wrapper to run a command in a temporary cgroup

DISCUSSION R. Diez 2 participants 2 messages Apr 4, 2022 – Apr 5, 2022 1 message not in the archive cgroup

lore
Share & tools
lore

R. Diez

author
#0 lore raw

Hi all:

I am looking for a wrapper script like this:

run-on-temporary-cgroup.sh cmd arg1 ... argn.

The tool should create a temporary cgroup, run the process inside, and return the same exit code as the user command.

I may want to limit the amount of memory the cgroup is allowed to consume. But more importantly, I want the tool to return when all processes in the cgroup (the user command an any children) have already terminated. This is apparently not so easy to achieve without cgroups.

Those requirements are similar to what systemd-run does, but I need an alternative which does not depend on systemd.

Even if you have systemd, systemd-run is rather convenient, but it is somewhat heavy weight and it has issues with some signals (like SIGTERM), and with escaping/quoting, depending on the mode (--scope vs --wait).

If there is no such a tool, can someone give me some hints on how to implement one? Is it possible in Shell or Perl? Or do I need to reach to C / C++? Is there anything similar I could take as basis?

Apologies if this is not the right mailing list. I could not find anywhere else to ask about cgroups matters.

Regards,
rdiez

A message that is not in the archive (sent off-list or not yet received): 0174490d-8679-3885-df31-e9f6c1e7205b-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

Hello.

Quoted 4 lines · R. Diez

run-on-temporary-cgroup.sh cmd arg1 ... argn.

The tool should create a temporary cgroup, run the process inside, and return the same exit code as the user command.

This sounds very much like systemd-run --scope.

Quoted 2 lines

Those requirements are similar to what systemd-run does, but I need an alternative which does not depend on systemd.

Fair enough.

Quoted 2 lines

Even if you have systemd, systemd-run is rather convenient, but it is somewhat heavy weight

Then a suggestion of using a container runtime is likely out of question too.

Quoted 3 lines

If there is no such a tool, can someone give me some hints on how to implement one? Is it possible in Shell or Perl? Or do I need to reach to C / C++? Is there anything similar I could take as basis?

There's also libcg [1] (whose cgroup v2 support is WIP AFAIK) with its cgexec utility. I don't know other tools (besides the named previously).

You can implement it in whatever environment that can interact with filesystem (and perhaps poll for events), it's all documented in [2] (alternatively [3], which I'd discourage for a new project).

HTH,
Michal

[1] https://github.com/libcgroup/libcgroup/blob/main/README
[2] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
[3] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/cgroups.html