Linux nice and renice Command With Examples
Introduction
Before we discuss Linux nice and renice command with examples, let's first take a look at-What are nice
and renice
Commands ?
The nice
and renice
commands in Linux are used to adjust the priority of a running process or to launch a new process with a specific priority. Priority levels determine the amount of CPU time allocated to a process by the operating system. By using these commands, system administrators can manage resource allocation and prioritize critical processes.
In this tutorial, you will study about the commands along with their examples. We will also address a few FAQs on Linux nice and renice command with examples.
How Does Nice and Renice Command Work on Linux?
Most processes operate with a priority of 0 by default. The nice command, however, helps users execute the process at a less priority. For this, the "nice" and "renice" commands' fundamental syntax is described below:
Syntax of nice Command:
nice -n [priority] [command]
The priority in the previously mentioned syntax is a number between -20 and 19. The higher the priority, the lower the numbers.
Syntax of renice Command:
renice -n [priority] -p [pid]
The "priority" is the same as previously stated in the renice syntax, and "pid" is the process ID of the process whose priority you desire to change.
Note: Normal users can only set priority to values between 0 and 19, while the root user can set priority to any value between -20 and 19.
Examples of how to use the nice and renice commands on Linux are as follows:
Example 1: Run a Program With a Lower Priority Value
The "run" command provides a priority value to enable user-driven application execution. Provide the program name and a priority value to run a lesser priority version of the application. To do this, specify "gnome-terminal" as the process name and 10 as the low priority value, as seen below.
nice -n 10 gnome-terminal
According to the output, the "gnome-terminal" process is running with a priority value of 10.
Example 2: Increase the Priority of a Specific Process
Specify the nice value to increase the priority of the specific process. In this case, specify the 10 value to speed up the procedure of "ps":
sudo nice --10 ps -l
The output shows the increasing priority of a specific process in the terminal.
Example 3: Change the Priority With a Process ID
Specify the process ID together with the priority value to change the priority of an active process with a process ID. Put a high priority value of "-5" on the process ID "2355" in our example:
renice -n -5 -p 2355
The output demonstrates that the process that is now active and has the process id "2355" has set the new priority to 5.
Example 4: Raise the Priority of Particular User
Include the user's name together with the priority value to increase the priority of all particular user processes. Let's give the particular user "roger" the priority value 5 in the following command:
sudo renice -n 5 -u roger
The output indicates that the revised priority value has been applied to all "roger" processes.
Example 5: Change the Priority of All Executing Processes of a Specific Group
Use the group name or ID to modify the priority of each process that is currently running. To do this, enter the group id in the command below with priority set to "8" and the "g" option:
renice -n 8 -g 2355
The output demonstrates that all currently running processes inside the particular group have a new priority.
Example 6: Change the Priority of All Same Name Processes
The "pgrep" command matches the process name through the "f" option to modify the priority of every process. Let's modify the priority of the process "firefox" by specifying its name:
renice -n 10 $(pgrep -f firefox)
The output demonstrates that the priority value has changed for all "firefox" processes.
FAQs on nice
and renice
Commands in Linux
How does the nice
command work in Linux?
The nice
command works by changing the priority of a process. It launches a new process with an altered priority level specified using the nice
value, which ranges from -20 (highest priority) to 19 (lowest priority).
How does the renice
command work in Linux?
The renice
command works by modifying the priority of a process identified by its process ID (PID) or username. It allows system administrators to increase or decrease the priority of a process within the range of -20 to 19.
Can I use the nice
and renice
commands to set negative priority values?
No, normal users are usually not allowed to set negative values with nice
or renice
. Negative values are reserved for privileged users (e.g., root) to assign higher priority to critical processes.
How do I launch a new process with a specific priority using the nice
command?
To launch a new process with a specific priority using nice
, you need to specify the priority value followed by the command you want to execute. For example, nice -n 10 command
launches the command
with a niceness value of 10.
Is it possible to renice multiple processes at once using the renice
command?
Yes, you can renice multiple processes at once using the renice
command by specifying multiple process IDs (PIDs) separated by spaces. This allows you to adjust the priority of multiple processes simultaneously.
How can I increase the priority of a running process using the renice
command?
To increase the priority of a running process using renice
, provide the process ID (PID) or username and the new priority level. Increasing the priority enables the process to receive a higher share of CPU time.
Can I renice a process to a higher or lower priority value than the currently running parent process?
No, you cannot renice a process to a priority value higher or lower than that of the currently executing parent process. This limitation ensures that user processes do not gain higher priority than system processes.
Conclusion
Linux provides the "nice" and "renice" commands to run programs with different scheduling priorities and restart processes that are already running. Using these commands, you can execute a program with a lower or higher priority value and adjust the priority of all active processes for a single user.
This tutorial explains the "nice" and "renice" commands on Linux using several practical examples.