nice
The command does not limit the percentage of CPU cycles that a process can consume per unit of time. The cpulimit command provides the best solution for both worlds. It limits the percentage of CPU cycles that a process can allocate per unit of time, and it is relatively easy to call.cpulimit
Commands are mainly useful for long-running and CPU-intensive processes. Compiling software and converting videos are common examples of long-running processes that can maximize the computer's CPU usage. Limiting the CPU usage of such processes will free up processor time for other tasks that may be running on the computer. Limiting CPU-intensive processes will also reduce power consumption and heat output, and may reduce system fan noise. The price of limiting the CPU usage of a process is that it takes more time to complete the run.
Install cpulimit
cpulimit
The commands are available in the default Fedora Linux repository. Run the following command to install on Fedora Linux system cpulimit
:
$ sudo dnf install cpulimit
View the documentation of cpulimit
cpulimit
There is no man page attached to the package. Use the following command to view cpulimit
the built-in documentation. The output results are provided below. But you may need to run this command on your own system to prevent the options from changing since this article was written.
$ cpulimit --help
Usage: cpulimit [OPTIONS…] TARGET
OPTIONS
-l, --limit=N percentage of cpu allowed from 0 to 800 (required)
-v, --verbose show control statistics
-z, --lazy exit if there is no target process, or if it dies
-i, --include-children limit also the children processes
-h, --help display this help and exit
TARGET must be exactly one of these:
-p, --pid=N pid of the process (implies -z)
-e, --exe=FILE name of the executable program file or path name
COMMAND [ARGS] run this command and limit it (implies -z)
Demo
In order to demonstrate cpulimit
the use of commands, a well-designed and computationally intensive Python script is provided below. The script first runs with no limit, and then runs with a limit of 50%. It calculates the value of the 42nd Fibonacci number . The script time
runs as a subprocess of the command in both cases to show the total time required to calculate the answer.
$ /bin/time -f '(computed in %e seconds)' /bin/python -c 'f = lambda n: n if n<2 else f(n-1)+f(n-2); print(f(42), end=" ")'
267914296 (computed in 51.80 seconds)
$ /bin/cpulimit -i -l 50 /bin/time -f '(computed in %e seconds)' /bin/python -c 'f = lambda n: n if n<2 else f(n-1)+f(n-2); print(f(42), end=" ")'
267914296 (computed in 127.38 seconds)
When running the first version of the command, you may hear the CPU fan on the computer spin up. But when you run the second version, you shouldn't. The first version of the command is not limited by the CPU, but it should not cause your computer to be paralyzed. It is written in such a way that it can only use at most one CPU core. Most modern PCs have multiple CPU cores, and when one of the CPUs is 100% busy, other tasks can be run at the same time without difficulty. To verify whether the first command maximizes one of your processors, run the top
command in a separate terminal window and press the 1
key. To exit the top
command can press the Q
key.
Setting a limit higher than 100% only makes sense for programs that can parallelize tasks . For such a program, an increment higher than 100% represents the full utilization of a CPU (200%=2 CPUs, 300%=3 CPUs, etc.).
Note that in the above example, the -i
options have been passed to the cpulimit
command. This is necessary because the command to be restricted is not cpulimit
a direct child of the command. Instead, it is time
a subprocess of the command, which in turn is cpulimit
a subprocess of the command. If there are no -i
options, cpulimit
only time
commands will be restricted .
Final note
If you want to restrict a graphics program that is launched from the desktop icon, please copy the program's .desktop
files (usually located in the /usr/share/applications
directory) to your ~/.local/share/applications
directory and modify the Exec
line accordingly. Then run the following command to apply these changes:
$ update-desktop-database ~/.local/share/applications
via: https://fedoramagazine.org/use-cpulimit-to-free-up-your-cpu/