This post will show you how to set Linux Process Priority with Nice and renice commands.

What Is the Nice Command Used For?

Put simply, Nice is a command-line utility in Linux that allows you to set processes’ “niceness” value.

Nice is used to start a process with a defined priority. When you start any program or process without any defined priority, nice set a default priority of 10. A niceness of 19 is the lowest priority, while -20 is the highest priority. The nice command is very useful when several processes demand more resources than the CPU.

What Is the Renice Command Used For?

Renice is very similar to nice and is used to change the priority of an already running process. If your system is running very slow due to a lengthy process, you can reduce or increase the priority of that process with the help of renice command.

Priority is a value that you can assign to each process, and the kernel uses this value to schedule the execution of the process.

  • 0-99 is the priority value used for real-time priority assignments.
  • 100-139 is the priority value that the users assign.

Step 1 – Display Nice Value of a Process

In Linux, when you start any process or program, it gets the default priority of 0.

You can use the ps or top command to display the priority of a running process.

To check the Nice value of the Nginx process, run the following command:

ps -fl -C nginx

You should see the priority of the Nginx process in the NI column as shown below:

F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
5 S www-data  3156  3155  0  80   0 - 21700 -      10:33 ?        00:00:00 nginx: worker process

To display the nice value of all running processes, use the top command:

top

You should see the nice value of all processes in the NI column.

Step 2 – Change the Priority of Process with Nice Command

The nice command can not change the priority of the running process. However, you can use the nice command to start any program with pre-defined priority.

For example, start a top program with a nice value 5:

nice -5 top

This will assign a priority value of 5 to the top.

Now, open another terminal and verify the priority for the top as shown below:

ps -fl -C top

You should see the priority of the top command in the NI column:

F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S vyom      7966  7277  0  85   5 -  7323 poll_s 11:41 pts/14   00:00:00 top

You don’t need root privileges when you set a priority value higher than 0. You will need root privileges if you want to increase the priority of any process by assigning a negative value.

For example, to increase the priority of the top command to -20, use the following command:

sudo nice --20 top

Step 3 – Change the Priority of Running Process with renice Command

As you know, the nice command cannot change the priority of any running process. In this case, you will need to use the renice command to change the priority of a running process.

Here, we will use the top process, which is already running.

First, verify the current priority of the top process using the following command:

ps -fl -C top

Sample output:

F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S vyom      7966  7277  0  85   5 -  7323 poll_s 11:41 pts/14   00:00:03 top

As you can see, the priority of the top process is 5.

Now, we will change the priority of the top process to -15.

First, find the PID of the top process with the following command:

pidof top

Sample output:

7966

Now, run the following command by specifying the priority value and PID of the top:

sudo renice -n -15 -p 7966

Sample output:

7966 (process ID) old priority 5, new priority -15

You can also change the priority of all processes a specific user owns.

For example, change the priority of all processes owned by the root user, run:

sudo renice -n 10 -u root

Conclusion

In this guide, you learned how to set and change the priority of any process using the nice and renice command. You can now easily increase and decrease the process priority per your needs. Start using nice and renice on your VPS hosting account from Altantic.Net.