What does the Systemd Resource Limits Calculator do?
The Systemd Resource Limits Calculator helps Linux sysadmins, SREs, and DevOps engineers configure cgroups resource control directives (MemoryMax, MemoryHigh, CPUQuota, TasksMax, OOMScoreAdjust) for Linux services and user slices. It outputs production [Service] unit file blocks and live systemctl set-property CLI commands.
Core Concepts
Understanding Systemd Cgroups Resource Directives:
MemoryMax: Hard memory limit for the unit cgroup. Exceeding this limit triggers the Linux kernel Out-of-Memory (OOM) killer.CPUQuota: Assigns a percentage of CPU execution time (e.g.200%grants 2 full CPU cores).TasksMax: Limits maximum number of concurrent tasks/threads in the cgroup (prevents fork bombs).OOMScoreAdjust: Adjusts OOM killer preference (-1000 makes process immune to OOM, 1000 makes it first choice for termination).
How to use the tool?
- Input Service Limits: Enter target CPU cores, RAM limits (
MemoryMax), and task limits. - Real-time Generation: The tool computes systemd directives (
CPUQuota=200%) and live CLI commands. - Copy Configuration: Click
Copyon the unit file snippet or pastesystemctl set-propertyinto your server.
Related Developer Utilities
If you manage Linux infrastructure, explore these complementary tools:
- Crontab to Systemd Timer: Convert 5-field cron jobs into native Systemd timer units.
- Linux Permissions Calculator: Convert chmod octal numbers (755, 644) into symbolic notation.
REST API Integration
Blueutils provides a free REST API endpoint (POST https://blueutils.com/api/linux/systemd-resource-calculator) to programmatically calculate systemd resource control directives.
API Request Parameters
| Name | Type | Description | Example |
|---|---|---|---|
unitName |
String | Target unit name. Default "my-service.service". |
"web-app.service" |
cpuCores |
Number | Target CPU core allocation. | 2 |
memoryMax |
String | Max memory limit string. | "1G" |
tasksMax |
String | Max tasks limit string. | "512" |
oomScoreAdjust |
Number | OOM score adjust (-1000 to 1000). | 0 |
oomPolicy |
String | OOM policy ("stop", "kill", "continue"). |
"stop" |
API Request Payload Examples
cURL
curl -X POST https://blueutils.com/api/linux/systemd-resource-calculator \
-H "Content-Type: application/json" \
-d '{
"unitName": "web-app.service",
"cpuCores": 2,
"memoryMax": "1G",
"tasksMax": "512"
}'Python
import requests
url = "https://blueutils.com/api/linux/systemd-resource-calculator"
payload = {
"unitName": "web-app.service",
"cpuCores": 2,
"memoryMax": "1G",
"tasksMax": "512"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String jsonPayload = """
{
"unitName": "web-app.service",
"cpuCores": 2,
"memoryMax": "1G",
"tasksMax": "512"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://blueutils.com/api/linux/systemd-resource-calculator"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}API Response Parameters
| Name | Type | Description | Example |
|---|---|---|---|
isValid |
Boolean | Indicates whether calculation succeeded. | true |
unitSnippet |
String | Formatted [Service] unit configuration snippet. |
"[Service]\nMemoryMax=1G\nCPUQuota=200%\nTasksMax=512" |
runtimeCommand |
String | Executable systemctl set-property CLI commands. |
"sudo systemctl set-property web-app.service MemoryMax=1G..." |
API Response Payload Examples
Success Response (HTTP 200 OK)
{
"isValid": true,
"unitName": "web-app.service",
"directives": [
"MemoryMax=1G",
"CPUQuota=200%",
"TasksMax=512"
],
"unitSnippet": "[Service]\nMemoryMax=1G\nCPUQuota=200%\nTasksMax=512",
"runtimeCommand": "sudo systemctl set-property web-app.service MemoryMax=1G\nsudo systemctl set-property web-app.service CPUQuota=200%\nsudo systemctl set-property web-app.service TasksMax=512"
}Validation Failure Response (HTTP 400 Bad Request)
{
"isValid": false,
"error": "Invalid OOMScoreAdjust value. Must be an integer between -1000 (OOM immune) and 1000 (OOM first choice)."
}Rate Limit Exceeded Response (HTTP 429 Too Many Requests)
{
"error": "API rate limit exceeded. Please wait or contact support@blueutils.com."
}Why use an API to calculate Systemd Resource Limits?
Integrating the Systemd Resource Control API into automation tools provides key benefits:
- Automated Service Hardening: Generate CPU and RAM limits for containerized Linux virtual machines automatically.
- Ansible & Infrastructure Provisioning: Compute unit drop-in overrides during VM provisioning.
Native Usage
How to generate and apply systemd resource control limits programmatically across environments:
Node.js (JavaScript)
function buildSystemdLimits(unitName = 'my-service.service', memoryMax = '1G', cpuQuota = '200%') {
return `sudo systemctl set-property ${unitName} MemoryMax=${memoryMax} CPUQuota=${cpuQuota}`;
}
console.log(buildSystemdLimits());Linux (Bash Shell)
#!/bin/bash
UNIT="my-service.service"
sudo systemctl set-property "$UNIT" MemoryMax=1G CPUQuota=200% TasksMax=512Python
def get_systemd_limits(unit="my-service.service", cpu_cores=2, mem="1G"):
quota = f"{int(cpu_cores * 100)}%"
return f"sudo systemctl set-property {unit} MemoryMax={mem} CPUQuota={quota}"
print(get_systemd_limits())Java
public class SystemdLimitsBuilder {
public static String buildLimitCmd(String unit, String memoryMax, String cpuQuota) {
return "sudo systemctl set-property " + unit + " MemoryMax=" + memoryMax + " CPUQuota=" + cpuQuota;
}
public static void main(String[] args) {
System.out.println(buildLimitCmd("my-service.service", "1G", "200%"));
}
}