{"id":13814,"date":"2019-04-06T09:36:15","date_gmt":"2019-04-06T09:36:15","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13814"},"modified":"2019-04-06T09:36:15","modified_gmt":"2019-04-06T09:36:15","slug":"limit-cpu-usage-of-a-process-in-linux-with-cpulimit-tool","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/limit-cpu-usage-of-a-process-in-linux-with-cpulimit-tool\/","title":{"rendered":"Limit CPU Usage of a Process in Linux with CPULimit Tool"},"content":{"rendered":"<p>In an earlier post, we\u2019ve explained\u00a0<a href=\"https:\/\/www.tecmint.com\/cputool-limit-linux-process-cpu-usage-load\/\" target=\"_blank\" rel=\"noopener\">CPUTool for limiting and controlling CPU utilization<\/a>\u00a0of 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\u00a0<strong>cpulimit<\/strong>.<\/p>\n<p><strong>Cpulimit<\/strong>\u00a0is used to restrict the CPU usage of a process in the same way as\u00a0<strong>CPUTool<\/strong>, however, it offers more usage options compared to its counterpart. One important difference is that cpulimit doesn\u2019t manage system load unlike\u00a0<strong>cputool<\/strong>.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/check-linux-cpu-information\/\" target=\"_blank\" rel=\"noopener\">9 Useful Commands to Get CPU Information on Linux<\/a><\/p>\n<h3>Install CPULimit to Limit CPU Usage Of a Process in Linux<\/h3>\n<p><strong>CPULimit<\/strong>\u00a0is available to install from default software repositories of\u00a0<strong>Debian\/Ubuntu<\/strong>\u00a0and its derivatives using a package management tool.<\/p>\n<pre>$ sudo apt install cpulimit\r\n<\/pre>\n<p>In\u00a0<strong>RHEL\/CentOS<\/strong>\u00a0and\u00a0<strong>Fedora<\/strong>, you need to first enable\u00a0<a href=\"https:\/\/www.tecmint.com\/how-to-enable-epel-repository-for-rhel-centos-6-5\/\" target=\"_blank\" rel=\"noopener\">EPEL repository<\/a>\u00a0and then install cpulimit as shown.<\/p>\n<p># yum install epel-release<br \/>\n# yum install cpulimit<\/p>\n<h4>Limiting Process CPU Usage With CUPLimit<\/h4>\n<p>In this sub section, we\u2019ll explain how cpulimit works. First, let\u2019s run a command (same\u00a0<strong>dd command<\/strong>\u00a0we 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).<\/p>\n<pre>$ dd if=\/dev\/zero of=\/dev\/null &amp;\r\n\r\n[1] 17918\r\n<\/pre>\n<p>Next, we can use the\u00a0<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">top<\/a>\u00a0or\u00a0<a href=\"https:\/\/www.tecmint.com\/glances-an-advanced-real-time-system-monitoring-tool-for-linux\/\" target=\"_blank\" rel=\"noopener\">glances<\/a>\u00a0tools which output the actual frequently updated state of a running Linux system, to watch the CPU usage of the command above.<\/p>\n<pre>$ top\r\n<\/pre>\n<div id=\"attachment_25991\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25991\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Monitor-CPU-Usage-in-Linux.png\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Monitor-CPU-Usage-in-Linux.png 818w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Monitor-CPU-Usage-in-Linux-768x430.png 768w\" alt=\"Monitor CPU Usage in Linux\" width=\"818\" height=\"458\" aria-describedby=\"caption-attachment-25991\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-25991\" class=\"wp-caption-text\">Monitor CPU Usage in Linux<\/p>\n<\/div>\n<p>Looking at the output above, we can see that the\u00a0<strong>dd<\/strong>\u00a0process is utilizing the highest percentage of CPU time\u00a0<strong>100.0%<\/strong>.<\/p>\n<p>But we can limit this using cputlimit as follows. The\u00a0<code>--pid<\/code>\u00a0or\u00a0<code>-p<\/code>\u00a0option is used to specify the PID and\u00a0<code>--limit<\/code>\u00a0or\u00a0<code>-l<\/code>\u00a0is used to set a usage percentage for a process.<\/p>\n<p>The command below will limit the\u00a0<strong>dd command<\/strong>\u00a0(<strong>PID 17918<\/strong>) to\u00a0<strong>50%<\/strong>\u00a0use of one CPU core.<\/p>\n<pre>$ sudo cpulimit --pid 17918 --limit 50  \r\n\r\nProcess 17918 detected\r\n<\/pre>\n<p>Once we run cpulimit, we can view the current CPU usage for the\u00a0<strong>dd command<\/strong>\u00a0with\u00a0<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">top<\/a>\u00a0or\u00a0<a href=\"https:\/\/www.tecmint.com\/glances-an-advanced-real-time-system-monitoring-tool-for-linux\/\" target=\"_blank\" rel=\"noopener\">glances<\/a>. From the output, the value ranges from (<strong>51.5%-55.0%<\/strong>\u00a0or slightly beyond).<\/p>\n<div id=\"attachment_25992\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25992\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Limit-CPU-Usage-of-Process-in-Linux.png\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Limit-CPU-Usage-of-Process-in-Linux.png 818w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Limit-CPU-Usage-of-Process-in-Linux-768x430.png 768w\" alt=\"Limit CPU Usage of Process in Linux\" width=\"818\" height=\"458\" aria-describedby=\"caption-attachment-25992\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-25992\" class=\"wp-caption-text\">Limit CPU Usage of Process in Linux<\/p>\n<\/div>\n<p>We can throttle its CPU usage for a second time as follows, this time lowering the percentage further as follows:<\/p>\n<pre>$ sudo cpulimit --pid 17918 --limit 20 \r\n\r\nProcess 17918 detected\r\n<\/pre>\n<p>As we did before, we can run top or glances to view the new CPU usage for the process, which will range from\u00a0<strong>20%-25.0%<\/strong>\u00a0or slightly beyond this.<\/p>\n<pre>$ top\r\n<\/pre>\n<div id=\"attachment_25993\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-25993\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Throttle-CPU-Usage-in-Linux.png\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Throttle-CPU-Usage-in-Linux.png 818w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/06\/Throttle-CPU-Usage-in-Linux-768x430.png 768w\" alt=\"Throttle CPU Usage in Linux\" width=\"818\" height=\"458\" aria-describedby=\"caption-attachment-25993\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-25993\" class=\"wp-caption-text\">Throttle CPU Usage in Linux<\/p>\n<\/div>\n<p><strong>Note<\/strong>: The shell becomes un-interactive \u2013 doesn\u2019t expect any user input when cpulimit is running. To kill it (which should stop the CPU usage limitation operation), press\u00a0<code>[Ctrl + C]<\/code>.<\/p>\n<p>To run cpulimit as a background process, use the\u00a0<code>--background<\/code>\u00a0or\u00a0<code>-b<\/code>\u00a0switch, freeing up the terminal.<\/p>\n<pre>$ sudo cpulimit --pid 17918 --limit 20 --background\r\n<\/pre>\n<p>To specify the number of CPU cores present on the system, use the\u00a0<code>--cpu<\/code>\u00a0or\u00a0<code>-c<\/code>\u00a0flag (this is normally detected automatically).<\/p>\n<pre>$ sudo cpulimit --pid 17918 --limit 20 --cpu 4\r\n<\/pre>\n<p>Rather than limit a process\u2019s CPU usage, we can kill it with the\u00a0<code>--kill<\/code>\u00a0or\u00a0<code>-k<\/code>\u00a0option. The default is signal sent to the process is\u00a0<strong>SIGCONT<\/strong>, but to send a different signal, use the\u00a0<code>--signal<\/code>\u00a0or\u00a0<code>-s<\/code>\u00a0flag.<\/p>\n<pre>$ sudo cpulimit --pid 17918 --limit 20 --kill \r\n<\/pre>\n<p>To exit if there is no suitable target process, or in case it dies, include the\u00a0<code>-z<\/code>\u00a0or\u00a0<code>--lazy<\/code>\u00a0like this.<\/p>\n<pre>$ sudo cpulimit --pid 17918 --limit 20 --kill --lazy\r\n<\/pre>\n<p>For additional information and usage options, view the cpulimit man page.<\/p>\n<pre>$ man cpulimit\r\n<\/pre>\n<p>Do check out the following useful guides for finding CPU info and CPU\/system performance monitoring.<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/find-linux-processes-memory-ram-cpu-usage\/\" target=\"_blank\" rel=\"noopener\">Find Top Running Processes by Highest Memory and CPU Usage in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/cpustat-monitors-cpu-utilization-by-processes-in-linux\/\" target=\"_blank\" rel=\"noopener\">Cpustat \u2013 Monitors CPU Utilization by Running Processes in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/corefreq-linux-cpu-monitoring-tool\/\" target=\"_blank\" rel=\"noopener noreferrer\">CoreFreq \u2013 A Powerful CPU Monitoring Tool for Linux Systems<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/find-linux-processes-memory-ram-cpu-usage\/\" target=\"_blank\" rel=\"noopener noreferrer\">Find Top Running Processes by Highest Memory and CPU Usage in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/command-line-tools-to-monitor-linux-performance\/\" target=\"_blank\" rel=\"noopener\">20 Command Line Tools to Monitor Linux Performance<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/linux-performance-monitoring-tools\/\" target=\"_blank\" rel=\"noopener\">13 Linux Performance Monitoring Tools \u2013 Part 2<\/a><\/li>\n<\/ol>\n<p>In comparison, after testing\u00a0<a href=\"https:\/\/www.tecmint.com\/cputool-limit-linux-process-cpu-usage-load\/\" target=\"_blank\" rel=\"noopener\">CPUTool<\/a>\u00a0and\u00a0<strong>CPULimit<\/strong>, we noticed that the former offers a more effective and reliable \u201cprocess CPU usage limitation\u201d functionality.<\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/limit-cpu-usage-of-a-process-in-linux-with-cpulimit-tool\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an earlier post, we\u2019ve explained\u00a0CPUTool for limiting and controlling CPU utilization\u00a0of 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\u00a0cpulimit. Cpulimit\u00a0is used to restrict the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/limit-cpu-usage-of-a-process-in-linux-with-cpulimit-tool\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Limit CPU Usage of a Process in Linux with CPULimit Tool&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13814","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13814"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13814\/revisions"}],"predecessor-version":[{"id":13815,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13814\/revisions\/13815"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}