I'm trying to run a large process on my machine with a timeout and maximum memory limit. For handling the memory usage, I created a cgroup and use this shell script to start the process:

#!/usr/bin/env bash

set -e
set -o pipefail
set -u
set -m

name="$1" && shift

"$@" &

if [[ -f /sys/fs/cgroup/memory/$name/cgroup.procs ]]; then
    echo "[MEMLIMIT] Adding $PID to $name cgroup..."
    echo "$PID" | tee "/sys/fs/cgroup/memory/$name/cgroup.procs"
    echo "[MEMLIMIT] Was not able to set memory-limiting, create cgroup first with crtmemcgrp.sh ..."

# Take job back to focus, can then be killed by Ctrl+C

Next, I also want to limit the process to running for at most 10 minutes. Hence, I use the following command line:

$ timeout 10m ./memlimit cgroup_name ./long_process [args]

My problem is, when the timeout expires, it kills the memlimit script, but not the long_process. My understanding is that the timeout command from coreutils creates its own process group and adds itself to it on startup.

As a result, all subsequent fork() calls should also be in the same process group and be killed when timeout kills the group.

What is happening here and how can I fix it?

Your Answer

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

Browse other questions tagged or ask your own question.