How to manipulate Process Priority in Linux using nice and renice Commands

This lesson we’re going to talk about how you prioritize processes (manipulate Process Priority in Linux) they’re running on a Linux system.

In my previous article, I had explained following concepts and I would request you to read that guides:

Introduction

Now remember Linux is a Multitasking Operating System.

Therefore, it rotates CPU time between each of the processes that are currently running on the system, creating an illusion really that all these processes are running concurrently.

Now because Linux is a Multitasking Operating System you can specify a priority level for each process running on a system.

And doing this determines how much CPU time a particular process is going to get in relation to the other processes that are running on the system.

Now by default Linux really doesn’t do a whole lot of prioritization.

It actually tries to equalize the amount of CPU time given to all the processes running on the system with a very limited number of exceptions.

However, there may be times when you need to adjust the priority assigned to a particular process.

Now depending upon how the system is deployed you may want one particular process to have a higher priority than the other processes running on the system.

Maybe the system is being used for a web server in which case you would want the web server process to have a higher priority than the other processes running on the system.

Well you can do this using several Linux utilities.

How to change Process Priority in Linux

So, in this lesson we’re going to talk about how you can set priorities with the nice command and how you can set the priority of a process that’s already running with the renice command.

Let’s begin by looking at how you can use the nice utility.

Set Priorities with the nice Command

The nice utility can be used to launch a new process on a Linux system with a customized priority level.

Now remember both the top and the ps utilities can be used to display information about the processes running on a Linux system.

And two of the values that will be displayed by ps and top are the PR value which is the Priority and the Nice Value which is the NI.

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND30738 helpdesk  20   0 2955736 105168  29692 S   8.3  13.0   2:56.70 gnome-shell31252 helpdesk  20   0  534516  21444  13576 S   1.3   2.6   0:11.46 gnome-terminal-    1 root      20   0  262096   5536   3184 S   0.0   0.7   0:11.67 systemd    2 root      20   0       0      0      0 S   0.0   0.0   0:00.05 kthreadd    3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp    4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp    6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-kblockd

Understanding the PR (Priority) Value

Here’s the key thing that you need to remember and it actually, is something that trips up a lot of new Linux System Administrators.

The higher the priority number right there the lower the priority of the process, It’s kind of counterintuitive.

In this example we have most of these processes running with a priority value of 20 but there are also processes that has a priority value of 0.

As you can see the kworker / 0: 0H-kblockd command has a priority of 0.

Because kworker / 0: 0H-kblockd uses a lower number this it’s actually a higher priority process than all of these other processes with a priority value of 20.

Just remember with the priority column the lower the number the higher the priority of the process.

Understanding the NI (Nice) Value

In addition to the priority you need to be familiar with the Nice Value, this is displayed in the NI column.

The NI value is the Nice Value of the process, it determines basically how nice the process is.

The nicer the process is the less CPU time it’s going to demand, the less nicer process is the more CPU time it’s going to demand.

The important thing to remember about the nice value is the fact that it’s factored into the kernels calculations that will determine the overall priority of the process.

Now the nice value for any Linux process can range between -20 to +19.

Now notice in the output of the top command here that most of these processes have exactly the same Nice value.

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND30738 helpdesk  20   0 2955736 105168  29692 S   8.3  13.0   2:56.70 gnome-shell31252 helpdesk  20   0  534516  21444  13576 S   1.3   2.6   0:11.46 gnome-terminal-    1 root      20   0  262096   5536   3184 S   0.0   0.7   0:11.67 systemd    2 root      20   0       0      0      0 S   0.0   0.0   0:00.05 kthreadd    3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp    4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp    6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-kblockd

They have the default value of 0 which basically causes them to have the same priority on the system.

Notice however that there are few exceptions such as the kworker/0:0H-kblockd Process, it has a Nice value of -20.

And as a result, it has a lower priority value than the other processes running on the system, which means it has a higher priority level on the system.

The important thing to remember is that the lower the Nice value is then the lower the priority value will be and therefore the higher the overall priority of the process on the system.

How to use the nice Command

Here’s another key thing you need to understand, that is the fact that you cannot directly manipulate the priority of a process, but you can indirectly manipulate it by customizing a processes Nice value.

The easiest way to do this is to actually set the process as Nice value when you initially load that process at the shell prompt and you do this using the nice command.

The syntax is shown here, you enter nice -n followed by the nice level that you want to assign to the process followed by the name of the actual command that you want to run.

Syntax: nice -n nice_level command

For example, let’s suppose that I wanted to launch the gedit program from the shell prompt.

gedit is a graphical text editor that can be used on Linux systems.

But I want to launch gedit with increased priority on the system because I take my text editing seriously.

Now by default if I just run gedit from the shell prompt it’s going to receive a priority of 80 on the system because it’s going to be assigned a nice value of 0.

# ps -elfF S UID          PID    PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD1 I root           8       2  0  60 -20 -     0 -      May11 ?        00:00:00 [mm_percpu_wq]1 S root           9       2  0  80   0 -     0 -      May11 ?        00:00:03 [ksoftirqd/0]1 I root         484       2  0  60 -20 -     0 -      May11 ?        00:00:00 [ttm_swap]0 S helpdesk   40156   30633  1  80   0 - 140623 x64_sy 22:56 ?       00:00:00 /usr/bin/gedit --gapplication-service0 R root       40187   32751  0  80   0 - 14455 -      22:56 pts/0    00:00:00 ps -elf4 S root       32746   31257  0  80   0 - 33371 -      14:52 pts/0    00:00:00 su4 S root       32751   32746  0  80   0 -  6730 -      14:52 pts/0    00:00:00 bash

That Nice value 0 is used by the kernel to calculate the overall priority of this process and using its various calculations that comes out to be 80.

Now because as I said I take my text editors very seriously, the best performance I can get when I’m editing text, I could adjust this processes priority level to a higher level by decreasing it’s Nice value.

For example, Here I entered nice -n -15 followed by the gedit command.

# nice -n -15 gedit

Remember that by default the nice value is 0, now I’m cranking it down to -15.

This will cause the priority number assigned to the process created by the command to decrease which as well increases its overall priority on the system and you can see that right here.

# ps -elfF S UID          PID    PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD1 I root           8       2  0  60 -20 -     0 -      May11 ?        00:00:00 [mm_percpu_wq]1 S root           9       2  0  80   0 -     0 -      May11 ?        00:00:03 [ksoftirqd/0]1 I root         484       2  0  60 -20 -     0 -      May11 ?        00:00:00 [ttm_swap]0 S helpdesk   40156   30633  1  65 -15 - 140623 x64_sy 22:56 ?       00:00:00 /usr/bin/gedit --gapplication-service0 R root       40187   32751  0  80   0 - 14455 -      22:56 pts/0    00:00:00 ps -elf4 S root       32746   31257  0  80   0 - 33371 -      14:52 pts/0    00:00:00 su4 S root       32751   32746  0  80   0 -  6730 -      14:52 pts/0    00:00:00 bash

After running the command my Nice value reduced to -15 and my priority number reduced to 65 from 80 which increases the overall priority of that process on the system.

Now with that said be aware that Linux is actually kind of hesitant to allow you to reduce the value of the Nice parameter for processes running on the system.

Because Linux is a true Multiuser Operating System, It is possible for multiple users on the same system to adjust the Nice values of their own processes.

And now human nature being what it is naturally what’s everyone going to do?

Everyone’s going to think well my processes are more important than everybody else’s so every user on the system is going to try to crank down the Nice value of their processes to increase its priority level on the system.

And we’re going to end up with a big tug-of-war.

Therefore, to keep this from happening Linux won’t let you adjust the Nice value of process below 0 unless you’re logged in as root.

Which means if you aren’t root you won’t be allowed to use a negative number with the nice command.

Set Priorities with the renice Command

Now the nice command works great for modifying the Nice value when you’re initially running a command that shell prompt to start a process.

But what do you do with the process that you want to modify is already running and you really don’t want to or can’t restarted.

Maybe it’s running some mission critical application and if you shut it off and restart it with a new Nice value you’re going to have the entire company coming after you with pitchforks.

Well in this situation you can’t use nice command instead you can use the renice command.

Instead of having to kill a process and restart it with nice to reset it’s Nice value.

You can use the renice command to adjust the Nice value of the process that’s already running and the Syntax is shown here.

How to use the renice Command

We run renice -n followed by the nice level we want to assign and this time we reference the Process ID (PID) number of the process whose nice value we want to change.

Syntax: renice -n nice_level PID

So, in this example, I have already got gedit running on the system it has a nice value of -15 and it’s Priority level is 65.

Let’s say because I did that, I’m using way more system resources than I should be just to edit text files.

In this situation I want to increase the Nice value of the gedit program making it a nicer more friendly program which will then lower its overall priority level on the system freeing up resources for other users to use.

And because I do still take my text editing seriously, I don’t want to actually exit out of gedit and reload it with Nice instead want to change its Nice value dynamically while it’s still running.

So, the first thing I do is run ps -elf to find out the Process ID (PID) of the gedit process which in this case is 40156.

# ps -elfF S UID          PID    PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD1 I root           6       2  0  60 -20 -     0 -      May13 ?        00:00:00 [kworker/0:0H-kblockd]0 S root       48359    1048  0  80   0 -  1820 hrtime 01:23 ?        00:00:00 sleep 604 S root       48360       1  0  80   0 - 61161 x64_sy 01:23 ?        00:00:00 /usr/libexec/fprintd0 S helpdesk   48404   30633  0  80   0 - 80189 x64_sy 01:23 ?        00:00:00 /usr/libexec/gnome-boxes-search-provider0 S helpdesk   48405   30633  4  80   0 - 238057 x64_sy 01:23 ?       00:00:00 /usr/bin/nautilus --gapplication-service0 S helpdesk   48407   30633  1  80   0 - 107318 x64_sy 01:23 ?       00:00:00 /usr/libexec/gnome-control-center-search-provider0 S helpdesk   40156   30633  1  65 -15 - 140624 x64_sy 01:23 ?       00:00:00 /usr/bin/gedit --gapplication-service0 R root       48458   32751  0  80   0 - 14455 -      01:23 pts/0    00:00:00 ps -elf

So, then I can run the renice command and I specify a nice value of 4.

# renice -n 4 40156

Now notice when I do I run the ps -elf command a second time and if I look at the priority and Nice value of the gedit process, now it’s much higher than it was before making gedit a much nicer friendlier program.

# ps -elfF S UID          PID    PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD1 I root           6       2  0  60 -20 -     0 -      May13 ?        00:00:00 [kworker/0:0H-kblockd]0 S root       48359    1048  0  80   0 -  1820 hrtime 01:23 ?        00:00:00 sleep 604 S root       48360       1  0  80   0 - 61161 x64_sy 01:23 ?        00:00:00 /usr/libexec/fprintd0 S helpdesk   48404   30633  0  80   0 - 80189 x64_sy 01:23 ?        00:00:00 /usr/libexec/gnome-boxes-search-provider0 S helpdesk   48405   30633  4  80   0 - 238057 x64_sy 01:23 ?       00:00:00 /usr/bin/nautilus --gapplication-service0 S helpdesk   48407   30633  1  80   0 - 107318 x64_sy 01:23 ?       00:00:00 /usr/libexec/gnome-control-center-search-provider0 S helpdesk   40156   30633  1  84   4 - 140624 x64_sy 01:23 ?       00:00:00 /usr/bin/gedit --gapplication-service0 R root       48458   32751  0  80   0 - 14455 -      01:23 pts/0    00:00:00 ps -elf

The Nice value is much higher than most of the other processes running on the system therefore my priority number is also higher meaning that my priority level is lower on the system.

I will get less CPU attention than the other processes running on the system.

One quick thing to remember is that when you’re using renice you’re still constrained by the same rule we talked about with nice.

And that is the fact that you have to be logged in as root if you want to adjust the Nice level of running process to a number that’s less than 0 in other words a -ve number.

If you’re logged in as a regular user you can only set its value to 0 or higher.

Conclusion

I hope that now you have a good understanding of How to manipulate Process Priority in Linux using nice and renice Commands

If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.