{"id":2968,"date":"2018-03-05T17:29:13","date_gmt":"2018-03-05T14:29:13","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-nice-command\/"},"modified":"2018-03-05T17:29:13","modified_gmt":"2018-03-05T14:29:13","slug":"linux-nice-and-renice-command-tutorial-7-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-nice-and-renice-command-tutorial-7-examples\/","title":{"rendered":"Linux nice and renice Command Tutorial (7 Examples)"},"content":{"rendered":"<div><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nice-and-renice-command-tutorial-7-examples.gif\" class=\"ff-og-image-inserted\" alt=\"\" title=\"\"><\/div>\n<p>The power of the Linux command line can be gauged from the fact that you can even easily tweak the scheduling priority of processes using command line tools. Yes, that&#8217;s possible, and in this tutorial we will discuss how to do that using <strong>nice<\/strong> and <strong>renice<\/strong> utilities.<\/p>\n<p>But before we do that, it&#8217;s worth mentioning that all examples in this article have been tested on an Ubuntu 16.04LTS system.<\/p>\n<h2 id=\"linux-nice-and-renice-commands\">Linux nice and renice commands<\/h2>\n<p>While the nice command lets you execute a program\/process with modified scheduling priority, the renice command allows you to change the scheduling priority of an already running process. Following is the generic syntax for\u00a0both these commands:<\/p>\n<p class=\"command\">nice [OPTION] [COMMAND [ARG]&#8230;]<\/p>\n<p class=\"command\">renice [-n] priority [[-p] pid &#8230;] [[-g] pgrp &#8230;] [[-u] user &#8230;]<\/p>\n<p>Here&#8217;s what their respective man pages have to say about them:<\/p>\n<pre>Nice: <br\/>Run COMMAND with an adjusted niceness, which affects process schedul?<br\/>ing. With no COMMAND, print the current niceness. Niceness values<br\/>range from -20 (most favorable to the process) to 19 (least favorable<br\/>to the process).<\/pre>\n<pre>Renice:<br\/>Renice alters the scheduling priority of one or more running processes.<br\/>The following who parameters are interpreted as process ID's, process<br\/>group ID's, or user names. Renice'ing a process group causes all pro?<br\/>cesses in the process group to have their scheduling priority altered.<br\/>Renice'ing a user causes all processes owned by the user to have their<br\/>scheduling priority altered. By default, the processes to be affected<br\/>are specified by their process ID's.<\/pre>\n<p>Following are some Q&amp;A-styled examples that will give you a better idea on how these tools work.<\/p>\n<h2 id=\"q-how-to-check-niceness-of-running-programsprocesses\">Q1. How to check niceness of running programs\/processes?<\/h2>\n<p>Yeah, one should first know how to check the existing scheduling priority before changing it. If it&#8217;s about the process you&#8217;re about to run, then you should know the default scheduling priority is always 0.<\/p>\n<p>For example, we executed the following process:<\/p>\n<p class=\"command\">.\/test-new<\/p>\n<p>And confirmed the priority using the following command:<\/p>\n<p class=\"command\">ps -lu himanshu | grep test-new<\/p>\n<p>Here, &#8216;himanshu&#8217; is the user who owns the &#8216;test-new&#8217; process. Following is the output the above command produced:<\/p>\n<pre>0 S \u00a01000 \u00a06306 \u00a06125 \u00a00 \u00a080 \u00a0 0 - \u00a0 508 hrtime pts\/18 \u00a0 00:00:00 test-new<\/pre>\n<p>The value in the 8th column is the nice value, and as you can see, it&#8217;s zero.<\/p>\n<h2 id=\"q-how-nice-command-works\">Q2. How nice command works?<\/h2>\n<p>So now coming to the point, how the nice command works? It&#8217;s easy &#8211; just use the tool in the following way:<\/p>\n<p class=\"command\">nice -PRIORITY COMMAND<\/p>\n<p>For example, if I want the scheduling priority to be 10, here&#8217;s how I can do that:<\/p>\n<p class=\"command\">nice -10 .\/test-new<\/p>\n<p>Following is the ps command output in this case, confirming the priority has been changed to 10.<\/p>\n<pre>0 S \u00a01000 \u00a06694 \u00a06125 \u00a00 \u00a090 \u00a010 - \u00a0 508 hrtime pts\/18 \u00a0 00:00:00 test-new<\/pre>\n<h2 id=\"q-how-to-make-nice-work-with-negative-values\">Q3. How to make nice work with negative values?<\/h2>\n<p>As already mentioned in the beginning,\u00a0niceness values range from -20 to 19, with the former being most favorable, while latter being least. In case you want to associate a negative nice value to the process, then you&#8217;ll have to use double hyphen.<\/p>\n<p>For example,\u00a0<\/p>\n<p class=\"command\">sudo nice &#8211;10 .\/test-new<\/p>\n<p>Please note that you need to have root privileges to associate a negative nice value to a process. And precisely for this reason, your ps command to confirm the new niceness should contain &#8216;root&#8217; instead of the other user name.<\/p>\n<p class=\"command\">$ ps -lu root | grep test-new<\/p>\n<p>Here&#8217;s the output this command produced in our case:<\/p>\n<pre>4 S 0 7054 7053 0 70 -10 - 508 - pts\/18 00:00:00 test-new<\/pre>\n<p>So you can see the process is now running with a nice value of -10.<\/p>\n<h2 id=\"q-how-to-add-a-set-integer-value-to-niceness\">Q4. How to add a set integer value to niceness?<\/h2>\n<p>You can adjust the nice value using the -n command line option, which adds a set integer value to the niceness. By default, this set value is 10, although you can pass a different value as well.<\/p>\n<pre>-n, --adjustment=N<\/pre>\n<p>For example:<\/p>\n<p class=\"command\">nice &#8211;adjustment=5 .\/test-new<\/p>\n<p>And here&#8217;s the output, the ps command produced in this case:<\/p>\n<pre>0 S 1000 7314 6125 0 85 5 - 508 hrtime pts\/18 00:00:00 test-new<\/pre>\n<p>So you can see a priority of 5 was set.<\/p>\n<h2 id=\"q-how-to-change-priority-of-running-processes\">Q5. How to change priority of running processes?<\/h2>\n<p>While the nice command is used while launching programs, the renice command lets you change the priority of a running process. For example, here&#8217;s how we changed the priority of the already-running &#8216;test-new&#8217; process from 5 to 15.<\/p>\n<p class=\"command\">renice -n 15 -p 7314<\/p>\n<p>The -p argument is for process ID. Following is the output the above command produced:<\/p>\n<pre>7314 (process ID) old priority 5, new priority 15<\/pre>\n<p>And the nice value indeed got changed to 15.<\/p>\n<h2 id=\"q-how-to-change-priority-for-all-processes-belonging-to-a-group\">Q6. How to change priority for all processes belonging to a group?<\/h2>\n<p>You can use the -g option for this. For example:<\/p>\n<pre>renice -n 20 -g howtoforge<\/pre>\n<p>The above command will change the priority of all processes belonging to the group &#8216;howtoforge&#8217;.<\/p>\n<h2 id=\"q-how-to-change-priority-for-all-processes-belonging-to-a-user\">Q7. How to change priority for all processes belonging to a user?<\/h2>\n<p>To change the priority for all programs associated to a user, use the -u option. For example:<\/p>\n<p class=\"command\">renice -n 5 -u himanshu<\/p>\n<p>Of course, a normal Linux command line user won&#8217;t usually get in situations where-in these tool are needed, but it&#8217;s always good to have knowledge of important command line tools, and nice and renice are among them. The good thing is that the learning curve associated with them isn&#8217;t steep at all. We have already discussed majority of options here. For more info on these utilities, head to their man pages &#8211; <a href=\"https:\/\/linux.die.net\/man\/1\/nice\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>\u00a0and <a href=\"https:\/\/linux.die.net\/man\/8\/renice\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.\u00a0<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The power of the Linux command line can be gauged from the fact that you can even easily tweak the scheduling priority of processes using command line tools. Yes, that&#8217;s possible, and in this tutorial we will discuss how to do that using nice and renice utilities. But before we do that, it&#8217;s worth mentioning &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2968","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2968"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2968\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}