Linux nice and renice Command Tutorial (7 Examples)

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's possible, and in this tutorial we will discuss how to do that using nice and renice utilities.

But before we do that, it's worth mentioning that all examples in this article have been tested on an Ubuntu 16.04LTS system.

Linux nice and renice commands

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 both these commands:

nice [OPTION] [COMMAND [ARG]...]
renice [-n] priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]

Here's what their respective man pages have to say about them:

Nice: 
Run COMMAND with an adjusted niceness, which affects process schedul?
ing. With no COMMAND, print the current niceness. Niceness values
range from -20 (most favorable to the process) to 19 (least favorable
to the process).
Renice:
Renice alters the scheduling priority of one or more running processes.
The following who parameters are interpreted as process ID's, process
group ID's, or user names. Renice'ing a process group causes all pro?
cesses in the process group to have their scheduling priority altered.
Renice'ing a user causes all processes owned by the user to have their
scheduling priority altered. By default, the processes to be affected
are specified by their process ID's.

Following are some Q&A-styled examples that will give you a better idea on how these tools work.

Q1. How to check niceness of running programs/processes?

Yeah, one should first know how to check the existing scheduling priority before changing it. If it's about the process you're about to run, then you should know the default scheduling priority is always 0.

For example, we executed the following process:

./test-new

And confirmed the priority using the following command:

ps -lu himanshu | grep test-new

Here, 'himanshu' is the user who owns the 'test-new' process. Following is the output the above command produced:

0 S  1000  6306  6125  0  80   0 -   508 hrtime pts/18   00:00:00 test-new

The value in the 8th column is the nice value, and as you can see, it's zero.

Q2. How nice command works?

So now coming to the point, how the nice command works? It's easy - just use the tool in the following way:

nice -PRIORITY COMMAND

For example, if I want the scheduling priority to be 10, here's how I can do that:

nice -10 ./test-new

Following is the ps command output in this case, confirming the priority has been changed to 10.

0 S  1000  6694  6125  0  90  10 -   508 hrtime pts/18   00:00:00 test-new

Q3. How to make nice work with negative values?

As already mentioned in the beginning, niceness 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'll have to use double hyphen.

For example, 

sudo nice --10 ./test-new

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 'root' instead of the other user name.

$ ps -lu root | grep test-new

Here's the output this command produced in our case:

4 S 0 7054 7053 0 70 -10 - 508 - pts/18 00:00:00 test-new

So you can see the process is now running with a nice value of -10.

Q4. How to add a set integer value to niceness?

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.

-n, --adjustment=N

For example:

nice --adjustment=5 ./test-new

And here's the output, the ps command produced in this case:

0 S 1000 7314 6125 0 85 5 - 508 hrtime pts/18 00:00:00 test-new

So you can see a priority of 5 was set.

Q5. How to change priority of running processes?

While the nice command is used while launching programs, the renice command lets you change the priority of a running process. For example, here's how we changed the priority of the already-running 'test-new' process from 5 to 15.

renice -n 15 -p 7314

The -p argument is for process ID. Following is the output the above command produced:

7314 (process ID) old priority 5, new priority 15

And the nice value indeed got changed to 15.

Q6. How to change priority for all processes belonging to a group?

You can use the -g option for this. For example:

renice -n 20 -g howtoforge

The above command will change the priority of all processes belonging to the group 'howtoforge'.

Q7. How to change priority for all processes belonging to a user?

To change the priority for all programs associated to a user, use the -u option. For example:

renice -n 5 -u himanshu

Conclusion

Of course, a normal Linux command line user won't usually get in situations where-in these tool are needed, but it'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't steep at all. We have already discussed majority of options here. For more info on these utilities, head to their man pages - here and here

Share this page:

6 Comment(s)