2 Ways to Limit CPU Usage of a Process in Linux

Channel: Linux
Abstract: 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 a

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 Also : How 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.

Wallpaper Engine - How to Reduce CP...

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

Wallpaper Engine - How to Reduce CPU Usage

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.

Ref From: linoxide
Channels:

Related articles