Maybe it’s a backup job, a software build, or a misbehaving application that starts using all available CPU resources.
In many cases, killing the process isn’t the best solution.
You may simply want to limit how much CPU or memory it can use so the rest of the system continues running normally.
In this article, you’ll learn four practical ways to control CPU and memory usage for Linux processes.
A Linux server is usually running many services at the same time, such as a web server, a database, scheduled jobs, monitoring tools, and background applications.
If one process suddenly starts consuming all available CPU resources, the entire system can become slow and unresponsive.
In some situations, killing the process is not the right solution.
For example, you may be running a large database backup, video conversion, software compilation, or data-processing job that needs to be completed successfully.
Instead of stopping it, you can limit how much CPU or memory it is allowed to use.
The same idea applies to memory usage.
If an application has a memory leak or is consuming more RAM than expected, setting limits can prevent it from affecting other services until a permanent fix is available.
When you need a quick and simple way to limit CPU consumption, cpulimit is one of the easiest tools available.
It works by temporarily pausing and resuming a process many times per second, keeping its average CPU usage below a specified percentage.
To install cpulimit on Linux, use the following appropriate command for your specific Linux distribution.
Before applying a limit, you need the process ID of the running application.
For example, to find the PID of Firefox:
Example output:
To restrict the process to 30% CPU usage:
Here’s what the options mean:
On a system with multiple CPU cores, the percentage is calculated per core.
For example, 30% on a four-core system is roughly equivalent to 7.5% of the machine’s total CPU capacity.
To run it in the background, add an ampersand (&) at the end:
If you receive a “Process not found” message, the process may have restarted with a new PID, so simply run pidof again and use the updated PID.
You can also launch a new application with a CPU limit already applied.
In this command:
This approach is particularly useful on shared systems where build jobs should not consume all available CPU resources.
While cpulimit is great for quick CPU throttling, it does not control memory usage.
In the next method, we’ll use Linux’s built-in scheduling tools to reduce a process’s CPU priority so it naturally gets fewer resources when the system is busy.
Unlike cpulimit, which actively restricts CPU usage, nice and renice work by influencing how the Linux scheduler allocates CPU time.
Instead of putting a strict cap on usage, they simply tell the system: “this process is less important than others”.
When the system is busy, lower-priority processes get fewer CPU cycles.
This makes nice ideal for background tasks like backups, archiving, log processing, or batch jobs that should not interfere with interactive work such as SSH sessions, editors, or web services.
The nice priority range goes from:
Regular users can only increase the nice value (lower priority), but only root can assign negative values.
To run a command with reduced priority, for example, to run the backup job with a nice value of 15, meaning the system will only give it CPU time when higher-priority processes are not using it.
If a process is already running, you can adjust its priority using renice:
Example output:
Here:
It is important to understand that nice does not limit CPU usage directly.
A low-priority process can still use 100% CPU if the system is idle.
However, the moment other processes need CPU time, the scheduler will prefer them over the low-priority job.
The cgroups (control groups) are the foundation of Linux resource management.
Unlike tools like cpulimit or nice, which operate at the user level, cgroups enforce limits directly in the Linux kernel, which means a process cannot bypass or “outgrow” these limits.
First, check whether your system is running cgroups v2:
If you see something like this, you are on v2:
Now create a new cgroup for your process:
To restrict the group to 512 MB of RAM:
To allow 20% of one CPU core (200000 out of 1000000):
Now assign a running process (PID 3271) to this cgroup:
The process is now hard-limited to 512MB RAM and 20% of one CPU core.
If it tries to allocate more memory than the ceiling, the kernel OOM killer fires and terminates it.
If you’d rather the process get throttled on memory rather than killed, set memory.swap.max to 0 to disable swap for the group and use a swap file strategy separately.
Either way, it cannot escape the cage.
If your process runs as a systemd service, this is usually the cleanest and most production-friendly option.
Instead of manually dealing with PIDs or cgroups, systemd handles everything for you using built-in resource controls.
Under the hood, systemd still uses cgroups v2, but it manages them automatically, so you don’t need to manually create or assign anything.
Let’s say you want to limit the nginx service so it cannot consume more than 50% CPU and 1 GB RAM.
So, you need to create a drop-in override file without editing the original unit:
Inside the file, add:
Reload systemd and restart the service:
Verify the limits are applied:
Example output:
What these settings mean:
For a softer memory limit that triggers a warning log but doesn’t kill the process, use MemoryHigh instead, which throttles allocation and logs the event without terminating the service.
If you see CPUQuota=0 in the output, the directive didn’t apply, so check that you saved the override to /etc/systemd/system/nginx.service.d/override.conf and that the daemon-reload ran cleanly.
The ulimit is a simple but powerful way to control what a shell session or a user is allowed to do.
Unlike cgroups or systemd, it does not manage services system-wide.
Instead, it works at the shell level, meaning it affects everything you start from that terminal session.
This makes it especially useful for shared servers where you want to protect the system from a single user running runaway processes or consuming too many resources.
To see what your current session is allowed to do:
Example output:
To restrict how much memory processes in this shell can use:
After setting this, any program launched from this terminal cannot exceed the defined memory limit.
To enforce limits across login sessions, you need to configure system-wide security limits.
Then add entries like:
What these limits mean:
The deploy user will see these limits enforced on every login after that.
ulimit changes in /etc/security/limits.conf require a new login session to take effect.
If you change them and test immediately in the same shell, nothing changes.
You now have five practical tools to control how much CPU and memory a Linux process can use:
Pick one process on your system right now (a backup job, a build script, a log shipper) and cap it with cpulimit or a systemd CPUQuota.
Watch what happens to system load before and after.
That ten-minute exercise will stick with you longer than reading about it.
Have you hit a situation where a single runaway process took down a shared server? Drop a comment below: what was the process and how did you recover it?
20 Must-Know Linux Commands for System Administrators
Create a Chat Server in Linux and 5 Other Terminal Tricks
witr: A Tool That Explains Why a Linux Process Is Running
How to Stop a Process Blocking a Port in Linux
How to Find Deleted Files Still Eating Disk Space
How to Copy Millions of Files Faster in Linux
Duf – A Better Linux Disk Monitoring Utility
MTR – A Network Diagnostic Tool for Linux
Bashtop – A Resource Monitoring Tool for Linux
How to Configure Custom Access and Error Log Formats in Nginx
SARG – Squid Analysis Report Generator and Internet Bandwidth Monitoring Tool
5 Tools to Scan a Linux Server for Malware and Rootkits
6 Useful Tools to Remember Linux Commands Forever
How to Install Particular Versions of Packages with Snap
Set Date and Time for Each Command You Execute in Bash History
30 Useful ‘ps Command’ Examples for Linux Process Monitoring
How to Install netstat Command in Linux
How to Find and Fix Failed Services in Linux
Top 5 Open-Source Productivity Tools for Linux
11 Best GUI Tools for Linux System Administrators in 2024
5 Best PDF Page Cropping Tools For Linux
10 Best Open Source Forum Software for Linux in 2024
15 Best Kali Linux Web Penetration Testing Tools
Top 5 Open Source Collaboration Platforms for Linux in 2024
—
**📚 Original Source:**
[How to Stop Linux Processes from Using Excessive CPU and RAM](https://www.tecmint.com/limit-cpu-memory-usage-linux-process/)
