How to limit the CPU usage by a Linux process ?

  • Post author:
  • Post category:General
How to limit the CPU usage by a Linux process ?

CPU2

Multi-tasking ability and faster response time are the most attractive features of Linux machines. Still some tasks may overburden the CPU and contribute to sluggish performance of machines. From running MySQL process to back up, the CPU is getting utilised in full. It is fine, if the machine is not engaged in any other processes or tasks. But what if the machine functions as a server or your main desktop? In this case, overburdening CPU for running a task will definitely result in slothful performance.

It is easy to quantitatively figure out CPU utilization and the processes that consume more CPU. But most users are unaware of how to limit the CPU usage of a particular process under Linux Operating System. Here, we will discuss how to force a particular process to limit the usage of CPU. First of all, let us discuss about the tools that are in vogue for the said purpose.

Cpulimit

Cpulimit is a simple program, which attempts to limit the CPU usage of a process (expressed in percentage, not in CPU time). They are useful to control CPU usage, while executing scheduled tasks that are not expected to overburden CPU. They act on real CPU usage without bothering nice value or other scheduling priorities. Also, it is able to adapt itself to the overall system load dynamically and quickly.

How it Works?

The cpulimit functions in userspace. So it doesn’t interfere with Linux scheduler. Here the target process, specified by pid, name, or command line is continuously paused and resumed by sending it SIGSTOP and SIGCONT signals. Signals are sent at appropriate moments, based on the limit specified by the user and the process statistics read from /proc.

Install cpulimit

# sudo apt-get install cpulimit (Ubuntu, Debian, etc)

# yum install cpulimit (RHEL, CentOS, etc)

Usage Syntax

Usage: cpulimit TARGET [OPTIONS…]

TARGET must be exactly one of these:

-p, –pid=N pid of the process

-e, –exe=FILE name of the executable program file

-P, –path=PATH absolute path name of the executable program file

OPTIONS

-b –background run in background

-l, –limit=N percentage of CPU allowed from 0 to 100 (mandatory)

-v, –verbose show control statistics

-z, –lazy exit if there is no suitable target process, or if it dies

-h, –help display this help and exit

Example:

* To limit CPU usage of the process to 25%, enter

# cpulimit -e <processname> -l 25

* To limit CPU usage of the process to 25% by using its PID, enter

# cpulimit -p <PID> -l 25

* You can also use absolute path name of the executable, enter

# cpulimit -P <Provide the absolute path> -l 25

Notes:

* If you rely on one processor the percentage can be limited between 1% – 100%. For example if you set the CPU usage percentage at 50%, your process cannot use more than 500 ms of CPU time for each second. If the machine has four processors then the percentage may vary from 0% -400%. In this case, setting the limit to 200% means the CPU can utilise half of the available power. In any case, the percentage is the same of what you see when you run the command ‘top’.

* 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 enjoy higher priority and precise control.

* cpulimit does not work with some specific kernel processes such as kcryptd, which ignores SIGSTOP signal.

This Post Has 7 Comments

  1. asia global

    Greeting…., Greate info about Linux…..also I believe, you spent a lot of time time write this things, because you made some good points there…
    just keep and finish your agenda step by step…regard

  2. DanialJCerdan

    Pretty! This has been an extremely wonderful article.
    Many thanks for providing this info.

  3. Melanotan

    Really enjoyed this article.Really looking forward to read more. Want more.

Leave a Reply