
nice Command in Linux
The nice command in Linux is a utility that allows users to run a program with a modified scheduling priority, known as niceness. By default, processes run with a niceness value of 0, but by using nice, you can adjust this value to influence the scheduling behavior of the process.
The niceness value ranges from -20 (which gives the process the highest priority and most favorable scheduling) to 19 (which gives it the lowest priority). When a process is started with a higher niceness value, it has a lower priority and is less likely to receive CPU time compared to processes with lower niceness values. This can be particularly useful for running less critical tasks in the background without them impacting the performance of more important tasks.
The command is typically used in scenarios where resource management is crucial, such as on shared systems or when performing resource-intensive tasks. For example, running a data backup or large compilation job with a high niceness value allows other users or critical system processes to operate smoothly without being starved of CPU time.
In addition, normal users in Linux systems can only increase the niceness value, which effectively reduces the process's priority. This means they can make their processes less likely to receive CPU time, ensuring that other processes have more resources.
On the other hand, superusers (or users with elevated privileges) have the ability to decrease the niceness value, thereby increasing the process's priority. This capability gives superusers greater control over process scheduling and allows them to allocate more CPU resources to critical tasks when necessary.
Table of Contents
Here is a comprehensive guide to the options available with the nice command −
Syntax of nice Command
The following is the general syntax for the nice command −
nice [OPTION] [COMMAND [ARG]...]
Where −
- OPTION − Adjust the priority of the command. If no option is specified, the default adjustment is 10.
- COMMAND − The command you want to run with the specified niceness.
- ARG − Arguments to pass to the command.
nice Command Options
The nice command in Linux systems has a few options that modify its behavior when adjusting the niceness of a process. Here is a detailed overview of the different options available for the nice command −
Options | Description |
---|---|
-n, --adjustment=N | Adds the specified integer value N to the niceness. The default adjustment is 10 if no value is specified. Positive values increase the niceness (lower priority), while negative values decrease the niceness (higher priority). |
--help | Displays help information and exits. |
--version | Outputs version information of the nice command and exits. |
Examples of nice Command in Linux
The following examples should give you a comprehensive understanding of how to use the nice command effectively in different scenarios −
Running a Command with Default Niceness Adjustment
To run a command with the default niceness adjustment (+10), you can use the following command −
sudo nice htop
This increases the niceness of htop by 10, making it less favorable for CPU scheduling.

Running a Command with a Specific Niceness Adjustment
To run a command with a specific niceness adjustment (+15), you can use the following command −
sudo nice -n 15 ls
This sets the niceness of ls to 15, further lowering its priority for CPU scheduling.

Running a Command with Higher Priority
To run a command with higher priority (-5), you can simply run −
sudo nice -n -5 gcc -o myprogram myprogram.c
Lowering the niceness to -5 increases gcc's priority, making it more favorable for CPU scheduling. Superuser privileges are required to decrease niceness.

Displaying the Current Niceness Value
To display the current niceness value of the shell, you can use the following command −
sudo nice
Running nice without any arguments will print the current niceness value of the shell.

Running a Script with Adjusted Niceness
To run a script with an adjusted niceness value (+20), you can use the following command −
sudo nice -n 20 ./backup.sh
This adjusts the niceness of backup.sh to 20, reducing its priority and ensuring other processes have more CPU time.

Running a Background Process with Low Priority
To run a background process with low priority, you can use the following command −
sudo nice -n 19 python3 myscript.py &
Setting the niceness to 19 gives myscript.py the lowest priority, allowing it to run in the background without interfering with other tasks.

Running a CPU-Intensive Task with Low Priority
To run a CPU-intensive task with low priority, you can simply run −
sudo nice -n 19 tar -czf archive.tar.gz /home/Neville/backup_dir
This command compresses the directory /home/Neville/backup_dir with the highest niceness, ensuring it doesn't impact the performance of other processes.

Running Multiple Commands with Different Niceness Values
To run multiple commands with different niceness values, you can use the following commands −
sudo nice -n 10 find / -name '*.txt' & nice -n 15 rsync -av /source /destination &
This runs find with a niceness of 10 and rsync with a niceness of 15, prioritizing find over rsync.

Running a Command and Checking Niceness
To run a command and immediately check its niceness, you can use the following command −
sudo nice -n 5 sleep 60 & sleep 1 && ps -o pid,nice,cmd
This runs sleep 60 with a niceness of 5, waits for a second, and then uses ps to display the niceness of running processes.

Running a Command and Logging Output
To run a command with adjusted niceness and log its output, you can use the following command −
sudo nice -n 10 rsync -av /source/ /destination/ > output.log 2>&1
This adjusts the niceness of the rsync command to 10. The rsync command synchronizes the contents of /source/ with /destination/, and both the standard output and error are redirected to output.log.

Exit Status −
- 125 − The nice command itself fails.
- 126 − The specified COMMAND is found but cannot be invoked.
- 127 − The specified COMMAND cannot be found.
- - − The exit status of the specified COMMAND, otherwise.
Conclusion
The nice command is a powerful and versatile utility in Linux systems, providing users with granular control over process scheduling priorities. By adjusting the niceness value, you can ensure that critical tasks receive adequate CPU resources while background or non-essential tasks do not adversely affect system performance.
The ability to modify niceness values-either to increase or decrease priority-enables efficient resource management, making it particularly valuable in environments with shared resources or high computational demands.
Understanding and utilizing the nice command effectively allows you to optimize system performance, balance workloads, and prevent resource contention.