Linux Nice & Renice Command with Examples

0 28

At any given point, there are hundreds of processes running in a system, most of which are created by the Linux operating system and some created by the logged-in user. Each running process has a priority assigned to it that determines how fast it is executed by the system. Higher priority processes are usually carried out earlier than low priority ones.

In Linux, the nice & renice commands are used to change the priority of a process, which, in effect, determines the urgency with which it is executed in the system.

The nice command configures the priority of a Linux process before it is started. Once started, you cannot change the priority using the nice command. This is where the renice command comes in. The renice command sets the priority of an already running process.

In this guide, we explore the Linux nice and renice commands and how they can be used to set priorities of processes.

Priority Values in Linux

In Linux systems, there are a total of 140 priorities with a nice value that ranges from -20 to +19 where +19 represents the lowest priority and -20 the highest priority. As you have noted, this is an inverse proportionality relationship between the nice value and the priority value.

By default, any Linux process created by a user has a nice value of 0.

How to Check the Nice Value of Running Processes?

There are a couple of ways of checking the nice value of running processes. You can use the ps command with the -l option, as follows:

The niceness value is denoted by the NI column header. The PRI column header denotes the actual priority of the process.

Additionally, you can invoke the top command to check the nice value of processes.

The default niceness value for processes started by a regular user is 0. In fact, you can easily verify the nice value for your terminal or shell by executing nice command without any arguments.

How Set the Nice Value of a Process?

The syntax of using the nice command is as follows:

$ nice -nice_value command-arguments

For example, to set a nice value of 5 to a command, run the command below.

Here, we are downloading the WordPress installation file using wget command with a nice value set to 5.

$ nice -5 wget https://wordpress.org/latest.zip

To set a negative value to a command, use a double hyphen, as shown. Be advised that you should run the command as root or sudo user as you execute this.

$ sudo nice –5 wget https://wordpress.org/latest.zip

How to Change the Nice Value Using Renice?

As earlier pointed out, the renice command changes the priority of a process that is already running in the Linux system. To do so, use the syntax:

$ sudo renice -n nice_value  -p pid_of_the_process

Suppose you wish to change the nice value of the gnome-terminal process. First, let’s check the nice value, as shown below:

$ ps -el | grep gnome-terminal

From the output, the nice value is 0. We are going to set it to 5, as shown below. Note 8721 is the PID of the gnome-terminal process

$ sudo renice -n 5  -p 8721

To set the priority of all processes belonging to a particular group, for example, sales, use the -g flag, as shown below:

$ sudo renice -n 5 -g sales

To modify the priority of all processes owned by a user, for example, winnie, use of the -u flag.

$ sudo renice -n 5 -u winnie

Summary

The nice and renice command allows users to change the priority of the processes in a Linux system. Note that the change in priority is temporary and is intended for that particular execution only.

Leave A Reply

Your email address will not be published.

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.