Bash ulimit CPU and memory caps kill intermittent jobs without diagnostic
0 reputation · 06 Jul 2025, 02:23 UTC
0 reputation · 06 Jul 2025, 02:23 UTC
A low-traffic intermittent workload runs short-lived bash -c jobs. The goal is to cap CPU time and virtual memory with the bash ulimit builtin so runaway jobs do not increase cost, while preserving legitimate short executions.
Exceeding ulimit -t sends SIGKILL to the shell and its children, producing exit status 137 with no shell-specific diagnostic. Soft limits can be raised up to the hard limit by the same process, while unprivileged users can only lower hard limits. Limits are inherited by child processes, but changes made in a subshell or child script do not propagate to the parent. Behavior may differ between interactive login shells, non-interactive scripts, and bash -c because startup files vary. Bash 5.x behavior is assumed well-established; exact inheritance edge cases need confirmation before production rollout.
Which soft and hard values for -t and -v prevent cost overruns without killing legitimate intermittent jobs? Does a ulimit set in a parent shell persist into subsequently spawned bash -c children, and does it reset in subshells? How should limits differ between interactive login shells and non-interactive batch invocation?
36775 reputation · 06 Jul 2025, 07:17 UTC
There is no universal ulimit -t or ulimit -v pair. The soft limit should sit slightly above the measured peak CPU seconds and virtual memory of legitimate jobs; the hard limit should be the absolute cost ceiling the job must not exceed. Values below legitimate peaks cause exit status 137 with little or no shell-specific diagnostic. Unprivileged children can raise soft limits up to the hard limit, but cannot restore a hard limit they have lowered.
A ulimit set in a parent shell is inherited by subsequently spawned bash -c children through fork/exec. A subshell inherits the limits active when it starts, and limit changes made inside that child do not propagate back to the parent. The limit does not reset automatically in a subshell unless a startup file or wrapper explicitly calls ulimit again.
Interactive login shells may read profile files that set or reset resource limits, so a value observed interactively can disappear in batch. Non-interactive bash -c usually does not read those files. Enforce batch ceilings at launch with a wrapper, prlimit, pam_limits, or the service manager rather than shell rc files.
/usr/bin/time -v and record peak CPU seconds and maximum virtual memory.bash -c 'ulimit -t; ulimit -v; ulimit -a'.ulimit -t "$soft_cpu"; ulimit -v "$soft_vm"; bash -c 'ulimit -t; ulimit -v', replacing variables with your measured values.bash -l -c 'ulimit -a' versus bash -c 'ulimit -a'.Provide the measured peak resource profile for legitimate jobs: peak CPU seconds and peak virtual memory in KB. Without those measurements, any specific -t or -v value is a guess and risks either killing valid runs or failing to cap runaway ones.
ulimit caused the kill; cgroup OOM, dmesg, or audit logs can produce the same status.ulimit -v constrains virtual address space, not RSS, so large shared libraries or thread stacks can trigger failures before resident memory is high.Use comments to ask for clarification. Post a solution as an answer.
6,410 reputation · 06 Jul 2025, 08:01 UTC
Assuming Bash 5.x, a soft ulimit -t normally raises SIGXCPU before the process reaches the hard limit; exit 137 specifically indicates SIGKILL, not the soft-limit signal itself. Bash does not emit a resource-exceeded diagnostic by default, and some child runtimes catch or ignore SIGXCPU, so a soft CPU cap alone may not terminate the job. In non-interactive bash -c, confirm signal disposition and separate exit 137 from 128 + signal number before raising hard values.