When using the nice command it is unable to alter the scheduling priority of processes that are already running. In this situation, the renice command excels. Here we shall examine the renice command in Linux with examples.
renice command
The renice command in Linux is used to change the scheduling priority of one or more running processes. Whereas we have seen nice is used for starting new processes with altered priority.
In Linux, niceness values range from -20 (highest priority) to 19 (lowest priority). Processes start with default 0 (base scheduling) priority unless otherwise specified.
Both nice and renice commands utilize the setpriority system call to change the priority of processes. The actual function sys_setpriority in the kernel source is not directly visible. This is because it's generated through macro expansion. If you run stace you can see those calls including getpriority and environment variables which are affected.
Regular users can only increase the niceness of their processes (make them lower priority). Decreasing the niceness (making processes higher priority) typically requires root privileges.
Syntax
renice [-n] <priority> [-p|--pid] <pid>...
renice [-n] <priority> -g|--pgrp <pgid>...
renice [-n] <priority> -u|--user <user>...
-n
: Specifies the new nice value (priority level).priority
: The new priority value.-p
: Refers to the process ID.-g
: Refers to the process group ID.-u
: Refers to the user's processes.
Examples
Adjust Priority of a Single Process
First, need to find the process ID (PID) and its current niceness value. You can use commands like ps, top, or htop for this purpose.
For example, you can use ps -l to show niceness along with other details:
ps -l
The 8th column shows the current nice value.
Once you have identified the PID and the current niceness, you can adjust the priority using -n <priority value> -p <PID>:
Example:
renice -n -19 -p 24016
The above command directly sets the priority (to -19 increased) of the process (PID 24016).
To verify run again ps -l
command:
You can see in the 8th column the nice values have been changed successfully of already running processes.
Here is another example that renices all processes belonging to user bob to a niceness of 20 (lowest priority):
renice 20 --pid `ps -o pid= --user bob`
Adjust Priority of a Process Group
Use -g option to adjust priority in the process group. This affects all processes within that group.
Example:
renice -n 10 -g 567
This decrease the priority of all processes in the process group with ID 567:
Adjust Priority of All Processes for a User
Using -o option you can change the priority of all processes owned by a specific user in Unix-like systems.
Example:
renice -n 7 -u linuxuser
The above command changes the priority of all processes owned by linuxuser to a niceness level of 7.
Comments