How to limit CPU, memory and tasks for dedicated server batch jobs
When a database shares a dedicated server with offline imports and report generation, trouble often starts with an unbounded batch job: concurrency increases, memory usage keeps growing, and online requests slow down. Exclusive use of the server eliminates resource sharing between tenants, but it does not automatically isolate processes on the same machine. This article uses Debian 12 to set resource boundaries for batch jobs with systemd and cgroup v2, then checks that those limits actually take effect.
HubAMD’s AMD EPYC 7543P is a single-socket processor with 32 cores and 64 threads. Two DDR4-3200 ECC memory options are available: 128GB and 256GB. The quotas below are for demonstration only, not recommended allocations for the whole server. Production limits should be derived from peak batch demand and the headroom required by online services.
1. Check the controllers before applying settings
After payment, retrieve your credentials from the console and connect over SSH. If multiple addresses are listed, use the first one. These operations require an account with sudo privileges; no particular provisioned username is assumed. First, confirm that the system uses the unified cgroup hierarchy:
stat -fc %T /sys/fs/cgroup
cat /sys/fs/cgroup/cgroup.controllers
systemd --version
The first command should return cgroup2fs, and the controller list should include cpu, memory, and pids. If the results differ, check the boot configuration and system environment first. Successfully writing a configuration file does not mean the kernel is enforcing its limits.
Also record the online service’s memory usage, response times, and task count under normal load. The acceptance criterion is whether services on the same server are affected, not whether the batch job can saturate the processor. For the first test, leave system-wide boot parameters unchanged and avoid changing database settings at the same time. This keeps the variables traceable if something goes wrong.
2. Separate the pressure threshold from the hard limit
CPUQuota=200% allows the job to consume at most the computing time equivalent of two logical CPUs. It does not mean 200% of the entire server’s CPU capacity, nor does it pin the job to two threads. The quota controls a time budget; threads can still be scheduled on different CPUs.
MemoryHigh is a pressure threshold. Above it, reclaim becomes more aggressive and the workload is throttled, but temporary overruns are allowed. MemoryMax is a hard limit; if reclaim cannot bring usage within that limit, an OOM event may be triggered within the cgroup. Leave a tested buffer between the two rather than setting them to the same value.
TasksMax counts kernel tasks, including threads. A single process that creates many threads can therefore hit the limit. Thread pools, database clients, and compression tools all need to be included in the budget.
A cgroup is not a virtual machine boundary. CPU, memory, and task limits do not automatically restrict disk throughput, network traffic, or file access, and they cannot guarantee that online services will remain completely unaffected.
Memory control also covers the page cache, so observing only the application’s heap size is not enough. When swap is available, MemoryMax is not a combined “memory plus swap” limit; swap behavior needs to be checked separately. Disk-intensive jobs require separate testing for I/O interference. A CPU quota is no substitute for storage acceptance testing.
3. Validate the boundaries with a transient service first
Install the interpreter needed for the example and create a dedicated service account:
sudo apt-get update
sudo apt-get install --no-install-recommends python3
sudo useradd --system --create-home --shell /usr/sbin/nologin batchjob
If the account already exists, skip creation and check what it is used for first. The following test allocates 256MiB of memory and runs a computation loop for approximately one minute. This is a controlled workload, not a benchmark, and it does not read or write business data files:
sudo systemd-run --unit=batch-limit-check \
--property=User=batchjob \
--property=CPUAccounting=yes \
--property=MemoryAccounting=yes \
--property=TasksAccounting=yes \
--property=CPUQuota=200% \
--property=MemoryHigh=2G \
--property=MemoryMax=3G \
--property=TasksMax=128 \
/usr/bin/python3 -c \
'import time; buf=bytearray(256*1024*1024); end=time.monotonic()+60
while time.monotonic()<end:
sum(range(10000))'
Check the service from another SSH session:
sudo systemctl show batch-limit-check.service \
-p ControlGroup -p CPUQuotaPerSecUSec \
-p MemoryHigh -p MemoryMax -p TasksMax \
-p MemoryCurrent -p TasksCurrent
sudo systemd-cgtop
sudo journalctl -u batch-limit-check.service --no-pager
This Python loop is single-threaded, so it cannot demonstrate that the quota equivalent to two logical CPUs has been fully used. The first pass should only verify cgroup membership, properties, and a clean exit. To verify CPU throttling, run a further test using a controlled parallel mode of the actual batch workload.
The transient service may be removed after the job finishes, so read usage values while it is running. Before running it again, confirm that the previous instance has finished. Do not resubmit a command to a unit with the same name while it is still running.
4. Turn the validated configuration into a persistent service
Once the transient test passes, write a persistent unit to /etc/systemd/system/batch-worker.service:
[Unit]
Description=Resource-limited batch worker
[Service]
Type=exec
User=batchjob
CPUAccounting=yes
MemoryAccounting=yes
TasksAccounting=yes
CPUQuota=200%
MemoryHigh=2G
MemoryMax=3G
TasksMax=128
ExecStart=/usr/bin/python3 -c "import time; time.sleep(120)"
Restart=no
The sleep command here is only for validating the persistent unit. Before connecting a real job, replace ExecStart with the absolute path of an already deployed and validated program, and ensure that the service account can read its input and write its output. Copying this example alone does not mean your batch workload is running.
sudo systemd-analyze verify /etc/systemd/system/batch-worker.service
sudo systemctl daemon-reload
sudo systemctl start batch-worker.service
sudo systemctl status batch-worker.service --no-pager
systemd does not automatically run ExecStart through a shell. Pipes, redirection, and environment expansion need to be implemented explicitly, preferably in a script that can be tested independently. Script permissions should be managed by the deployment account; do not grant write access to everyone just to make execution easier.
Keep Restart=no until the job supports idempotent execution, checkpoints, and a retry limit. Restarting indefinitely as soon as the memory limit is exceeded turns a single failure into sustained resource pressure. Start the service manually for acceptance testing before deciding whether to connect it to a scheduled trigger.
5. Test failure paths and keep a rollback option
Distinguish three types of resource failure
CPU throttling typically increases completion time without necessarily causing an exit error. While the service is running, obtain its path from ControlGroup, read the corresponding cpu.stat, and compare the increases in nr_throttled and throttled_usec. Do not rely on cumulative totals alone.
Investigate memory problems using both the service logs and the same cgroup’s memory.events. high, max, oom, and oom_kill mean different things: reaching the pressure threshold does not mean a process has been killed. If task creation fails, check TasksCurrent and pids.events, and also rule out the program’s own thread-pool limits.
Roll out gradually, and roll back gradually
Production acceptance testing should cover at least normal input, the largest expected input, and invalid input. Record the exit status, output completeness, and online service response times. A successful exit does not prove that the results are correct. Failed output should go to a separate staging area and only be published after validation, so downstream consumers do not read a partial report.
Save the unit file and logs from the current test before rolling back. To contain a problem, run systemctl stop batch-worker.service. If an adjustment is needed, restore a validated configuration, run daemon-reload, and start the service again. Removing all limits should not be the first fix.
Change only one variable per round. Establish the job’s peak demand first, then decide whether to raise a boundary, reduce concurrency, or split the input. Keep a final acceptance record containing the configuration, input size, exit status, and impact on business services, so the next change can be checked through the same process.
Frequently asked questions
Does CPUQuota=200% mean twice the CPU capacity of the server?
No. systemd treats one logical CPU's execution time as 100%. A quota of 200% allows up to two logical CPUs' worth of execution time, without pinning threads or guaranteeing capacity.
Why configure both MemoryHigh and MemoryMax?
MemoryHigh triggers memory reclaim and throttling and may be exceeded temporarily. MemoryMax is a hard boundary; if reclaim cannot keep usage within it, an OOM may occur inside the cgroup.
HubAMD AMD EPYC 7543P dedicated server
From $448/month