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:
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.
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.
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:
Suppose you wish to change the nice value of the gnome-terminal process. First, let’s check the nice value, as shown below:
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
To set the priority of all processes belonging to a particular group, for example, sales, use the -g flag, as shown below:
To modify the priority of all processes owned by a user, for example, winnie, use of the -u flag.
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.