- 2 Ways to Limit CPU Usage of a Process in Linux
- 1) cpulimit
- 2) cputool
- More Articles You May Like
- 1 thought on “2 Ways to Limit CPU Usage of a Process in Linux”. add one
- Limit CPU Usage of a Process in Linux with CPULimit Tool
- Install CPULimit to Limit CPU Usage Of a Process in Linux
- Limiting Process CPU Usage With CUPLimit
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- Linux Limit CPU Usage Per Process
- Install cpulimit
- A note about Debian / Ubuntu Linux users
- How do I use cpulimit?
- Root vs Normal User Account
- A Note About SMP (Multicore / MultiCpu) Systems
- Related Throttling Utilities
- Recommended Readings:
- How to Limit CPU Usage of a Process on Linux
- Limiting CPU Usage with nice and renice:
- Limiting CPU Usage with CGROUPS:
2 Ways to Limit CPU Usage of a Process in Linux
While administering your Linux system you need to limit the CPU usage of a process because it can consume more CPU usage and affects the performance of the whole system. This tutorial I will show how you can control CPU time of a process on Centos 7 and Ubuntu 16.
1) cpulimit
cpulimit is a nice command-line tool written in C programming language for limiting the CPU usage of a process. It is not present by default so we need to install it.
To install cpulimit on Ubuntu 16.04, do as below
Install on centos 7
We limit the CPU usage by using the —limit or -l option to set a usage percentage for a process. Before limiting the CPU usage, you need to find the process or the command that is using all your CPU time. There are three ways to use cpulimit, by acting
- on the executable program with the —executable or -e option
- on the absolute path name of the file with the —path or -P option
- on the PID of the process with the —pid or -p option
So, we will first check the process/program which consumes the CPU time
Then you can use the following command with the name of the program.
You can see the tool has detected automatically the process id of the program. You can limit the cpu usage with the process id as below
And you can see the result below
You can run cpulimit in background by using the —background or -b option
2) cputool
You can use cputool which also uses cpulimit to reduce CPU usage.This can be also done by using the load limit. cputool is not present by default so you need to install it on Ubuntu 16
To use cputool, you need to respect the syntax below
With the same example, you can use the cputool as below
The command will take the control of your terminal so you can use it in background as below:
You can see the result as below
With these tools, you can easily control the CPU usage of a process when it is already running. You don’t need to kill the processes to reduce the usage but you can simply assign a percentage to not exceed.
More Articles You May Like
1 thought on “2 Ways to Limit CPU Usage of a Process in Linux”. add one
When I am using cpulimit in this format:
cpulimit -e program -l %cpu,
it will detect the 1st program that name matches, and have it restricted. What happen I have a bunch of process running of the same program .
In my environment, this is the command I have executed:
cpulimit -e qemu-system-x86_64 -l 3%,
And this the output of top call:
top — 22:42:34 up 23:22, 3 users, load average: 26.43, 26.69, 25.88
Tasks: 214 total, 5 running, 209 sleeping, 0 stopped, 0 zombie
%Cpu(s): 84.0 us, 16.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 30879432 total, 8491104 used, 22388328 free, 103928 buffers
KiB Swap: 2095100 total, 0 used, 2095100 free. 596156 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5379 gns3 20 0 984580 672640 14340 S 12.3 2.2 128:02.35 qemu-system-x86
8431 gns3 20 0 984660 578792 14356 R 12.3 1.9 6:06.29 qemu-system-x86
8440 gns3 20 0 984660 580036 14468 S 12.3 1.9 6:08.20 qemu-system-x86
4740 gns3 20 0 984660 670428 14296 T 3.3 2.2 127:44.46 qemu-system-x86
.
«.
As you can see, only the process of id 4740 has been restricted.
Источник
Limit CPU Usage of a Process in Linux with CPULimit Tool
In an earlier post, we’ve explained CPUTool for limiting and controlling CPU utilization of any process in Linux. It allows a system administrator to interrupt execution of a process (or process group) if the CPU/system load goes beyond a defined threshold. Here, we will learn how to use a similar tool called cpulimit.
Cpulimit is used to restrict the CPU usage of a process in the same way as CPUTool, however, it offers more usage options compared to its counterpart. One important difference is that cpulimit doesn’t manage system load unlike cputool.
Install CPULimit to Limit CPU Usage Of a Process in Linux
CPULimit is available to install from default software repositories of Debian/Ubuntu and its derivatives using a package management tool.
In RHEL/CentOS and Fedora, you need to first enable EPEL repository and then install cpulimit as shown.
# yum install epel-release
# yum install cpulimit
Limiting Process CPU Usage With CUPLimit
In this sub section, we’ll explain how cpulimit works. First, let’s run a command (same dd command we looked at while covering cputool) which should result into a high CPU percentage, in the background (note that the process PID is printed out after running the command).
Next, we can use the top or glances tools which output the actual frequently updated state of a running Linux system, to watch the CPU usage of the command above.
Monitor CPU Usage in Linux
Looking at the output above, we can see that the dd process is utilizing the highest percentage of CPU time 100.0%.
But we can limit this using cputlimit as follows. The —pid or -p option is used to specify the PID and —limit or -l is used to set a usage percentage for a process.
The command below will limit the dd command (PID 17918) to 50% use of one CPU core.
Once we run cpulimit, we can view the current CPU usage for the dd command with top or glances. From the output, the value ranges from (51.5%-55.0% or slightly beyond).
Limit CPU Usage of Process in Linux
We can throttle its CPU usage for a second time as follows, this time lowering the percentage further as follows:
As we did before, we can run top or glances to view the new CPU usage for the process, which will range from 20%-25.0% or slightly beyond this.
Throttle CPU Usage in Linux
Note: The shell becomes un-interactive – doesn’t expect any user input when cpulimit is running. To kill it (which should stop the CPU usage limitation operation), press [Ctrl + C] .
To run cpulimit as a background process, use the —background or -b switch, freeing up the terminal.
To specify the number of CPU cores present on the system, use the —cpu or -c flag (this is normally detected automatically).
Rather than limit a process’s CPU usage, we can kill it with the —kill or -k option. The default is signal sent to the process is SIGCONT, but to send a different signal, use the —signal or -s flag.
To exit if there is no suitable target process, or in case it dies, include the -z or —lazy like this.
For additional information and usage options, view the cpulimit man page.
Do check out the following useful guides for finding CPU info and CPU/system performance monitoring.
In comparison, after testing CPUTool and CPULimit, we noticed that the former offers a more effective and reliable “process CPU usage limitation” functionality.
This is according to the percentage range of CPU usage observed after running both tools against a given process. Try out both tools and add your thoughts to this article using the feedback form below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
Linux Limit CPU Usage Per Process
I don’t want background process to eat all my CPU. I know how to find out CPU utilization but how do I limit the cpu usage of a process under Linux operating system? How do I force a process to limit itself to 25% only?
You can use cpulimit program that attempts to limit the cpu usage of a process. Limits are expressed in percentage and not in cpu time. cpulimit does not act on the nice value or other scheduling priority stuff, but on the real cpu usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.
Install cpulimit
Type the following commands to install latest stable release:
# cd /tmp
# wget ‘http://downloads.sourceforge.net/cpulimit/cpulimit-1.1.tar.gz’
# tar -zxvf cpulimit-1.1.tar.gz
# cd cpulimit-1.1
# make
# cp cpulimit /usr/local/sbin/
# rm -rf cpulimit*
A note about Debian / Ubuntu Linux users
Type the following command to install cpulimit:
$ sudo apt-get update
$ sudo apt-get install cpulimit
How do I use cpulimit?
To limit CPU usage of the process called firefox to 30%, enter:
# cpulimit -e firefox -l 30
To limit CPU usage of the process to 30% by using its PID, enter:
# cpulimit -p 1313 -l 30
To find out PID of the process use any of the following:
# ps aux | less
# ps aux | grep firefox
# pgrep -u vivek php-cgi
# pgrep lighttpd
You can also use absolute path name of the executable, enter:
# cpulimit -P /opt/firefox/firebox -l 30
Where,
- -p : Process PID.
- -e : Process name.
- -l : percentage of CPU allowed from 0 to 100.
- -P: absolute path name of the executable program file.
Root vs Normal User Account
From the project webpage:
cpulimit should run at least with the same user running the controlled process. But it is much better if you run cpulimit as root, in order to have a higher priority and a more precise control.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
A Note About SMP (Multicore / MultiCpu) Systems
Again quoting from the project webpage:
If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power. In any case, the percentage is the same of what you see when you run top.
Related Throttling Utilities
- ionice utility – Avoid sudden outburst of backup shell script / program disk I/O.
- Limit disk I/O for rsync tool.
- Linux nice command: Run Process With Modified Scheduling Priority ( nicenesses )
- renice command: Change the Priority of a Already Running Process
Recommended Readings:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to Limit CPU Usage of a Process on Linux
You can set how much CPU a single process is allowed to have. If the process really needs a lot of CPU power, you can run a few commands to give it all the idle CPU cycles (CPU cycles that you don’t need). That way, you will never have to sit and stare at your unresponsive computer for long.
In this article, I will show you how to Limit CPU usage of a process on Linux. I am going to use CentOS 7 in this article. But any modern Linux distribution should work. So, Let’s get started.
Limiting CPU Usage with nice and renice:
On Linux, the priorities of each running processes can be changed. You can set higher priorities to the process which is more important to you than a process that is hogging your CPU for no good reason.
Every process on Linux has a nice value. The value of nice determines which process has higher priorities and which has lower. Nice value can be between -20 to 19. A process with the nice value of -20 will have the highest priority and will use the most CPU cycles. A process with the nice value 19 will have the lowest priority and will use the CPU when no other processes are using it only.
There are two ways to set the nice value of a process. You can either start a process with the nice command to set a nice value while starting the process. Or you can use the renice command to set a nice value after a process has started.
To set a nice value when you start a process, run the process as follows:
NOTE: Here NICE_VALUE can be anything from -20 to 19 and COMMAND_TO_RUN is any command that you want to run with the nice value of NICE_VALUE.
For example, let’s say, you want to run the sleep command with the nice value of 14. Run the command as follows:
Now you can verify whether the nice value is set correctly using the top command. You can list all the processes that you started (as your login user) with the following command:
As you can see, the nice value of the process is set to 14.
Now if you wish to change the nice value of your existing processes, then all you need is the process ID (PID) of the process of which you want to change the nice value. You can use the ps aux command or the top command to find the process ID or PID.
Then you can run renice command as follows to change the nice value of an existing process:
As you can see, the nice value of the process with PID 6422 is changed.
Limiting CPU Usage with CGROUPS:
The full form of CGROUPS is Control Groups. It is a Linux kernel features used to limit resources to process groups such as (CPU, memory, permissions and many more) on Linux.
All you have to do is, create a new process group and add your processes that you want to limit resources to, to that group. Simple!
CGROUPS management tools are not installed on CentOS 7 by default. But it is available in the official package repository of CentOS 7.
First update the YUM package repository cache with the following command:
Now install CGROUPS management tools with the following command:
Now press y and then press .
It should be installed.
You can limit CPU usage of a single group. For example, you can use CGROUPS to tell a process within a CGROUP to use let’s say 100ms out of every 1000ms (or .1s out of every 1s) of CPU time.
First create a CGROUP with the following command:
NOTE: Here, cpulimit is the group name that controls the cpu usage.
Now, you have to set cpu.cfs_period_us and cpu.cfs_quota_us property on the cpulimit group.
For this example, 1000ms (milliseconds) or 1000000us (microseconds) should be set to cpu.cfs_period_us property and 100ms or 100000us should be set to the cpu.cfs_quota_us property.
Run the following commands to set these properties to the cpulimit group:
Now you can run the following command to check whether all the properties are correctly set:
NOTE: Here, cpulimit is the name of the CGROUP and cpu is the resource that I am limiting.
As you can see, cpu.cfs_period_us and cpu.cfs_quota_us are correctly set.
Now whatever process you add to cpulimit CGROUP will use 1/10 th (100000/1000000 = 1/10 = 0.1 = 10%) of the total CPU cycles.
Now to limit CPU of a process, start the program or command with cgexec as follows:
NOTE: Here, YOUR_COMMAND can be any valid Linux commands.
To prove that it actually works, first I am going to run the following command without CGROUPS and then with CGROUPS and show you the results.
As you can see, without CGROUPS, the command uses 90% of the total CPU.
Then, I ran the same command with CGROUPS as follows:
As you can see, the CPU usage is 10% at maximum. The process is not using more than that.
So that’s how you use CGROUPS to limit CPU usage of a process on Linux. Thanks for reading this article.
Источник