‘nice’ & ‘renice’ commands in Linux
The ‘nice’ and ‘renice’ commands in Linux are used to adjust the priority of processes running on the system. The priority of a process determines how much CPU time it is allocated relative to other processes.
Whereas the ‘renice’ command is used to alter the priority of an already existing process, the ‘nice’ command is used to launch a process with a modified priority.
Every process on a Linux system is given the “nice” value of 0, which denotes a normal priority, by default. The range of a “lovely” value is -20 (highest priority) to 19 (lowest priority).
A process can be given a higher or lower priority by using the “nice” command to launch it with a different “nice” value. Using the process ID (PID) and the new “nice” value, the “renice” command can be used to modify the priority of an active process.
Here’s how to use ‘nice’ and ‘renice’ commands in Linux:
Using ‘nice’
Use the “nice” command to start a process with a different priority. The syntax of the ‘nice’ command is as follows:
nice [OPTION] [COMMAND [ARG]…]
The options that can be used with the ‘nice’ command are:
- -n: Sets the nice value, which is a number between -20 (highest priority) and 19 (lowest priority).
- -adjustment: Increases or decreases the nice value by the specified adjustment. The adjustment can be a positive or negative number.
To use the ‘nice’ command to launch a process with a lower priority, you can specify a positive nice value. For example:
nice -n 10 firefox
This launches the Firefox browser with a nice value of 10, which is a lower priority than the default value.
To launch a process with a higher priority, you can specify a negative nice value. For example:
nice -n -10 gcc program.c
This launches the GCC compiler with a nice value of -10, which is a higher priority than the default value.
Using ‘renice’
The ‘renice’ command is used to change the priority of a running process. The syntax of the ‘renice’ command is as follows:
renice [OPTIONS] PRIORITY [PID]…
The options that can be used with the ‘renice’ command are:
- -g: Changes the priority of all processes in the specified process group.
- -u: Changes the priority of all processes the specified user owns.
To use the ‘renice’ command to change the priority of a process, you need to specify the process ID (PID) of the process and the new priority value. For example:
renice -n 10 -p 1234
This changes the priority of the process with PID 1234 to a nice value of 10.
The “renice” command can also be used to alter the order of all the processes that a certain user owns. For example:
renice -n -5 -u alice
This changes the priority of all processes owned by the user “alice” to a nice value of -5.
Linux ‘nice’ and ‘renice’ commands Examples
The ‘nice’ and ‘renice’ commands in Linux are used to adjust the priority of a running process. ‘nice’ is used to launch a new process with a specified priority level, and ‘renice’ is used to change the priority of an already running process.
-
Using the ‘nice’ command to adjust process priority
To launch a new process with a specified priority level, use the ‘nice’ command followed by the command you want to run, and the priority level you want to assign to it. The priority level ranges from -20 (highest priority) to 19 (lowest priority).
Example:
nice -n 10 command-to-run
This will run the ‘command-to-run’ process with a priority level of 10.
Output: No output is produced; the process will run with the assigned priority level.
-
Using the ‘renice’ command to change process priority
Use the ‘renice’ command along with the priority level you want to assign, the process ID of the running process, and any other necessary arguments to modify the priority of an already running process.
Example:
renice -n 5 1234
This will change the priority level of the process with ID 1234 to 5.
Output: No output is produced; the priority of the specified process is updated.
-
Checking the priority of a process
To check the priority of a running process, use the ‘ps’ command followed by the process ID, and the ‘o’ option to specify the priority column.
Example:
ps -p 1234 -o pid,comm,nice
This will display the process ID, name, and priority level of the process with ID 1234.
Output:
-
Changing the priority of multiple processes with the ‘renice’ command
To change the priority of multiple processes at once, use the ‘renice’ command followed by the priority level and the process IDs separated by spaces.
Example:
renice -n 5 1234 5678
This will change the priority level of the processes with IDs 1234 and 5678 to 5.
Output: No output is produced, and the priorities of the specified processes are updated.
-
Using the ‘nice’ command to launch a process with a higher priority
Use the ‘nice’ command followed by the command you wish to run and a priority level less than 0 to start a process with a higher priority level than the default.
Example:
nice -n -10 command-to-run
This will run the ‘command-to-run’ process with a priority level of -10, which is higher than the default priority level.
Output: No output is produced, the process will run with the assigned priority level.
-
Checking the priority of all running processes
To check the priority of all running processes, use the ‘ps’ command followed by the ‘aux’ options, and the ‘o’ option to specify the priority column.
Example:
ps aux –sort=-ni -o pid,comm,ni
This will display the process IDs, names, and priority levels of all running processes, sorted by priority level in descending order.
Output:
Note: Negative priority levels indicate higher priority.
-
Using the ‘renice’ command with user-defined priorities
To use user-defined priority levels with the ‘renice’ command, you can create a file containing the desired priorities and then use the ‘-f’ option to specify the file when running the ‘renice’ command.
Here are the steps to create and use a file with user-defined priorities:
- Create a file containing the desired priorities. The file should contain one line per process ID, with the process ID followed by a space and the desired priority level. For example:
1234 5
5678 10
This file assigns a priority level of 5 to process ID 1234 and a priority level of 10 to process ID 5678.
- Save the file with a descriptive name, such as ‘mypriorities.txt’.
- Use the ‘renice’ command with the ‘-f’ option to specify the file containing the desired priorities. For example:
sudo renice -f mypriorities.txt
This command will read the ‘mypriorities.txt’ file and adjust the priorities of the specified processes accordingly.
- Verify that the priorities have been updated by using the ‘ps’ command to display information about the processes. For example:
ps -eo pid,nice,cmd
This command will display the process ID, the priority level (in the ‘nice’ column), and the command associated with each process. You should see that the priorities for the specified processes have been updated to the levels specified in the ‘mypriorities.txt’ file.
Wrapping Up
These commands are useful in situations where you need to adjust the CPU usage of certain processes. For example, you may want to give a video rendering process a higher priority so that it completes faster, or you may want to lower the priority of a background process to reduce its impact on the system’s performance.
Overall, the ‘nice’ and ‘renice’ commands provide a way to manage the priorities of processes on a Linux system and ensure that CPU resources are allocated efficiently.
You can learn about linux more deeply by clicking the link below
https://linuxiron.com/what-is-linux-a-whole-introduction/
https://linuxiron.com/echo-command-in-linux/
The renice command modifies one or more running processes’ nice values. The nice value is the process’s system scheduling priority in decimal form. The impacted processes’ process IDs are by default used to identify them.
On Unix and Unix-like operating systems, including Linux, there is an application called nice. It corresponds exactly to the identical kernel call. With the nice command, you can give a shell script or utility a specific CPU priority, giving it more or less CPU time than other processes.
A process’s pleasant value can be set using the nice command before it is launched. Sadly, this tool’s version does not allow for the modification of a process’s nice value while it is in operation. A process that is currently executing can have its nice value modified using the renice command.
Simply put, those phrases indicate if the process’s default pleasant value has been modified. A process that has been run with the nice command (or had its niceness altered by renice) is referred to as being “niced,” whereas a process that has not been run with pleasant is referred to as being “un-niced.”