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.

Refer AlsoHow to Limit Process at User Level on Linux

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

# apt-get install cpulimit

Install on centos 7

# yum install epel-release
# yum install cpulimit

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 -eoption
cpulimit -e program -l %cpu
  • on the absolute path name of the file with the --path or -Poption
cpulimit -P path -l %cpu
  • on the PID of the process with the--pid or -poption
cpulimit -p process_pid -l %cpu

So, we will first check the process/program which consumes the CPU time

# top

Then you can use the following command with the name of the program.

# cpulimit -e vmware-vmx -l 40
Process 3185 detected

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

# cpulimit -p 3185 -l 40
Process 3185 detected

And you can see the result below

You can run cpulimit in background by using the--background or -boption

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

# apt install cputool

To use cputool, you need to respect the syntax below

cputool --cpu-limit %cpu -p process_pid

With the same example, you can use the cputool as below

# cputool --cpu-limit 30 -p 3185

The command will take the control of your terminal so you can use it in background as below:

# cputool --cpu-limit 30 -p 3185 &
[1] 16808

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.

Alain Francois 12:05 am

About Alain Francois

IT Linux administrator passionate of free and open source software, I work on Linux Systems since some years on installations and deployments of certain solutions. I like to share my experiences with a wider audience by training and conferences.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.