{"id":13893,"date":"2019-04-06T13:54:22","date_gmt":"2019-04-06T13:54:22","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13893"},"modified":"2019-04-06T13:54:22","modified_gmt":"2019-04-06T13:54:22","slug":"how-to-set-linux-process-priority-using-nice-and-renice-commands","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-set-linux-process-priority-using-nice-and-renice-commands\/","title":{"rendered":"How to Set Linux Process Priority Using nice and renice Commands"},"content":{"rendered":"<p>In this article, we\u2019ll briefly explain the\u00a0<strong>kernel scheduler<\/strong>\u00a0(also known as the\u00a0<strong>process scheduler<\/strong>), and\u00a0<strong>process priority<\/strong>, which are topics beyond the scope of this guide. Then we will dive into a little bit of\u00a0<a href=\"https:\/\/www.tecmint.com\/rhcsa-exam-boot-process-and-process-management\/\" target=\"_blank\" rel=\"noopener\">Linux process management<\/a>: see how to run a program or command with modified priority and also change the priority of running Linux processes.<\/p>\n<p><strong>Read Also:<\/strong>\u00a0<a href=\"https:\/\/www.tecmint.com\/monitor-linux-processes-and-set-process-limits-per-user\/\" target=\"_blank\" rel=\"noopener\">How to Monitor Linux Processes and Set Process Limits on a Per-User Basis<\/a><\/p>\n<h4>Understanding the Linux Kernel Scheduler<\/h4>\n<p>The kernel scheduler is a unit of the kernel that determines the most suitable process out of all runnable processes to execute next; it allocates processor time between the runnable processes on a system. A runnable process is one which is waiting only for CPU time, it\u2019s ready to be executed.<\/p>\n<p>The scheduler forms the core of multitasking in Linux, using a priority-based scheduling algorithm to choose between the runnable processes in the system. It ranks processes based on the most deserving as well as need for CPU time.<\/p>\n<h4>Understanding Process Priority and Nice Value<\/h4>\n<p>The kernel stores a great deal of information about processes including process priority which is simply the scheduling priority attached to a process. Processes with a higher priority will be executed before those with a lower priority, while processes with the same priority are scheduled one after the next, repeatedly.<\/p>\n<p>There are a total of\u00a0<strong>140<\/strong>\u00a0priorities and two distinct priority ranges implemented in Linux. The first one is nice value (<strong>niceness<\/strong>) which ranges from\u00a0<code>-20<\/code>\u00a0(highest priority value) to\u00a0<code>19<\/code>\u00a0(lowest priority value) and the default is\u00a0<code>0<\/code>, this is what we will uncover in this guide. The other is the real-time priority, which ranges from\u00a0<strong>1<\/strong>\u00a0to\u00a0<strong>99<\/strong>\u00a0by default, then\u00a0<strong>100<\/strong>\u00a0to\u00a0<strong>139<\/strong>\u00a0are meant for user-space.<\/p>\n<p>One important characteristic of Linux is dynamic priority-based scheduling, which allows nice value of a processes to be changed (increased or decreased) depending on your needs, as we\u2019ll see later on.<\/p>\n<h3>How to Check Nice Value of Linux Processes<\/h3>\n<p>To see the nice values of processes, we can use utilities such as\u00a0<a href=\"https:\/\/www.tecmint.com\/ps-command-examples-for-linux-process-monitoring\/\" target=\"_blank\" rel=\"noopener\">ps<\/a>,\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\/install-htop-linux-process-monitoring-for-rhel-centos-fedora\/\" target=\"_blank\" rel=\"noopener\">htop<\/a>.<\/p>\n<p>To view processes nice value with ps command in user-defined format (here the\u00a0<code>NI<\/code>\u00a0column shows niceness of processes).<\/p>\n<pre>$ ps -eo pid,ppid,ni,comm\r\n<\/pre>\n<div id=\"attachment_27113\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27113\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/View-Processes-Nice-Values.png\" alt=\"View Linux Processes Nice Values\" width=\"542\" height=\"344\" aria-describedby=\"caption-attachment-27113\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-27113\" class=\"wp-caption-text\">View Linux Processes Nice Values<\/p>\n<\/div>\n<p>Alternatively, you can use top or htop utilities to view Linux processes nice values as shown.<\/p>\n<pre>$ top\r\n$ htop\r\n<\/pre>\n<div id=\"attachment_27114\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27114\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/View-Process-Nice-Values-using-top.png\" alt=\"Check Linux Process Nice Values using Top Command\" width=\"752\" height=\"344\" aria-describedby=\"caption-attachment-27114\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-27114\" class=\"wp-caption-text\">Check Linux Process Nice Values using Top Command<\/p>\n<\/div>\n<div id=\"attachment_27115\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27115\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/View-Process-Nice-Values-using-htop.png\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/View-Process-Nice-Values-using-htop.png 802w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/View-Process-Nice-Values-using-htop-768x366.png 768w\" alt=\"Check Linux Process Nice Values using Htop Command\" width=\"802\" height=\"382\" aria-describedby=\"caption-attachment-27115\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-27115\" class=\"wp-caption-text\">Check Linux Process Nice Values using Htop Command<\/p>\n<\/div>\n<h4>Difference Between PR or PRI and NI<\/h4>\n<p>From the\u00a0<strong>top<\/strong>\u00a0and\u00a0<strong>htop<\/strong>\u00a0outputs above, you\u2019ll notice that there is a column called\u00a0<code>PR<\/code>\u00a0and\u00a0<code>PRI<\/code>\u00a0receptively which show the priority of a process.<\/p>\n<p>This therefore means that:<\/p>\n<ul>\n<li><code>NI<\/code>\u00a0\u2013 is the nice value, which is a user-space concept, while<\/li>\n<li><code>PR<\/code>\u00a0or\u00a0<code>PRI<\/code>\u00a0\u2013 is the process\u2019s actual priority, as seen by the Linux kernel.<\/li>\n<\/ul>\n<h5>How To Calculate PR or PRI Values<\/h5>\n<pre>Total number of priorities = 140\r\nReal time priority range(PR or PRI):  0 to 99 \r\nUser space priority range: 100 to 139\r\n<\/pre>\n<p>Nice value range (NI): -20 to 19<\/p>\n<pre>PR = 20 + NI\r\nPR = 20 + (-20 to + 19)\r\nPR = 20 + -20  to 20 + 19\r\nPR = 0 to 39 which is same as 100 to 139.\r\n<\/pre>\n<p>But if you see a\u00a0<code>rt<\/code>\u00a0rather than a number as shown in the screen shot below, it basically means the process is running under real time scheduling priority.<\/p>\n<div id=\"attachment_27116\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27116\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/Linux-rt-example.png\" sizes=\"auto, (max-width: 842px) 100vw, 842px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/Linux-rt-example.png 842w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/Linux-rt-example-768x383.png 768w\" alt=\"Linux rt Process\" width=\"842\" height=\"420\" aria-describedby=\"caption-attachment-27116\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-27116\" class=\"wp-caption-text\">Linux rt Process<\/p>\n<\/div>\n<h3>How to Run A Command with a Given Nice Value in Linux<\/h3>\n<p>Here, we will look at how to prioritize the CPU usage of a program or command. If you have a very CPU-intensive program or task, but you also understand that it might take a long time to complete, you can set it a high or favorable priority using the\u00a0<strong>nice command<\/strong>.<\/p>\n<p>The syntax is as follows:<\/p>\n<pre>$ nice -n niceness-value [command args] \r\nOR\r\n$ nice -niceness-value [command args] \t#it\u2019s confusing for negative values\r\nOR\r\n$ nice --adjustment=niceness-value [command args]\r\n<\/pre>\n<p>Important:<\/p>\n<ul>\n<li>If no value is provided, nice sets a priority of 10 by default.<\/li>\n<li>A command or program run without nice defaults to a priority of zero.<\/li>\n<li>Only root can run a command or program with increased or high priority.<\/li>\n<li>Normal users can only run a command or program with low priority.<\/li>\n<\/ul>\n<p>For example, instead of starting a program or command with the default priority, you can start it with a specific priority using following nice command.<\/p>\n<pre>$ sudo nice -n 5 tar -czf backup.tar.gz .\/Documents\/*\r\nOR\r\n$ sudo nice --adjustment=5 tar -czf backup.tar.gz .\/Documents\/*\r\n<\/pre>\n<p>You can also use the third method which is a little confusing especially for negative niceness values.<\/p>\n<pre>$ sudo nice -5 tar -czf backup.tar.gz  .\/Documents\/*\r\n<\/pre>\n<h3>Change the Scheduling Priority of a Process in Linux<\/h3>\n<p>As we mentioned before, Linux allows dynamic priority-based scheduling. Therefore, if a program is already running, you can change its priority with the\u00a0<strong>renice command<\/strong>\u00a0in this form:<\/p>\n<pre>$ renice -n  -12  -p 1055\r\n$ renice -n -2  -u apache\r\n<\/pre>\n<div id=\"attachment_27117\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27117\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/Change-Process-Priority.png\" alt=\"Change Process Priority\" width=\"592\" height=\"116\" aria-describedby=\"caption-attachment-27117\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-27117\" class=\"wp-caption-text\">Change Process Priority<\/p>\n<\/div>\n<p>From the sample\u00a0<strong>top<\/strong>\u00a0output below, the niceness of the\u00a0<strong>teamspe+<\/strong>\u00a0with PID\u00a0<strong>1055<\/strong>\u00a0is now\u00a0<code>-12<\/code>\u00a0and for all processes owned by user apache is\u00a0<code>-2<\/code>.<\/p>\n<p>Still using this output, you can see the formula\u00a0<strong>PR = 20 + NI<\/strong>\u00a0stands,<\/p>\n<pre>PR for ts3server = 20 + -12 = 8\r\nPR for apache processes = 20 + -2 = 18\r\n<\/pre>\n<div id=\"attachment_27118\" class=\"wp-caption aligncenter\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27118\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/09\/Watch-Processes-Nice-Values.png\" alt=\"Watch Processes Nice Values\" width=\"752\" height=\"477\" aria-describedby=\"caption-attachment-27118\" data-lazy-loaded=\"true\" \/><\/p>\n<p id=\"caption-attachment-27118\" class=\"wp-caption-text\">Watch Processes Nice Values<\/p>\n<\/div>\n<p>Any changes you make with\u00a0<strong>renice command<\/strong>\u00a0to a user\u2019s processes nice values are only applicable until the next reboot. To set permanent default values, read the next section.<\/p>\n<h3>How To Set Default Nice Value Of a Specific User\u2019s Processes<\/h3>\n<p>You can set the default nice value of a particular user or group in the\u00a0<strong>\/etc\/security\/limits.conf<\/strong>\u00a0file. It\u2019s primary function is to define the resource limits for the users logged in via PAM.<\/p>\n<p>The syntax for defining a limit for a user is as follows (and the possible values of the various columns are explained in the file):<\/p>\n<pre>#&lt;domain&gt;   &lt;type&gt;  &lt;item&gt;  &lt;value&gt;\r\n<\/pre>\n<p>Now use the syntax below where hard \u2013 means enforcing hard links and soft means \u2013 enforcing the soft limits.<\/p>\n<pre>&lt;username&gt;  &lt;hard|soft&gt;  priority  &lt;nice value&gt;\r\n<\/pre>\n<p>Alternatively, create a file under\u00a0<strong>\/etc\/security\/limits.d\/<\/strong>\u00a0which overrides settings in the main file above, and these files are read in alphabetical order.<\/p>\n<p>Start by creating the file\u00a0<strong>\/etc\/security\/limits.d\/tecmint-priority.conf<\/strong>\u00a0for user\u00a0<strong>tecmint<\/strong>:<\/p>\n<pre># vi \/etc\/security\/limits.d\/tecmint-priority.conf\r\n<\/pre>\n<p>Then add this configuration in it:<\/p>\n<pre>tecmint  hard  priority  10\r\n<\/pre>\n<p>Save and close the file. From now on, any process owned by\u00a0<strong>tecmint<\/strong>\u00a0will have a nice value of\u00a0<strong>10<\/strong>\u00a0and PR of\u00a0<strong>30<\/strong>.<\/p>\n<p>For more information, read the man pages of\u00a0<strong>nice<\/strong>\u00a0and\u00a0<strong>renice<\/strong>:<\/p>\n<pre>$ man nice\r\n$ man renice \r\n<\/pre>\n<p>You might also like to read these following articles about Linux process management.<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/find-and-kill-running-processes-pid-in-linux\/\" target=\"_blank\" rel=\"noopener\">How to Find and Kill Running Processes in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/how-to-kill-a-process-in-linux\/\" target=\"_blank\" rel=\"noopener\">A Guide to Kill, Pkill and Killall Commands to Terminate a Process in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/linux-system-monitoring-troubleshooting-tools\/\" target=\"_blank\" rel=\"noopener\">How to Monitor System Usage, Outages and Troubleshoot Linux Servers<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/cputool-limit-linux-process-cpu-usage-load\/\" target=\"_blank\" rel=\"noopener\">CPUTool \u2013 Limit and Control CPU Utilization of Any Process in Linux<\/a><\/li>\n<\/ol>\n<p>In this article, we briefly explained the kernel scheduler, process priority, looked at how to run a program or command with modified priority and also change the priority of active Linux processes. You can share any thoughts regarding this topic via the feedback form below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/set-linux-process-priority-using-nice-and-renice-commands\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we\u2019ll briefly explain the\u00a0kernel scheduler\u00a0(also known as the\u00a0process scheduler), and\u00a0process priority, which are topics beyond the scope of this guide. Then we will dive into a little bit of\u00a0Linux process management: see how to run a program or command with modified priority and also change the priority of running Linux processes. Read &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-set-linux-process-priority-using-nice-and-renice-commands\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Set Linux Process Priority Using nice and renice Commands&#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-13893","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\/13893","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=13893"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13893\/revisions"}],"predecessor-version":[{"id":13894,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13893\/revisions\/13894"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}