In Linux, the nice
and renice
commands are used to adjust the priority of processes.
1. nice
Command:
The nice
command allows you to start a new process with a specified priority (nice value). The “nice” value determines the priority of the process, where lower values mean higher priority, and higher values mean lower priority. The default nice value is 0.
Syntax:
nice -n <priority> <command>
<priority>
: The nice value (range from -20 (highest priority) to 19 (lowest priority)).<command>
: The command you want to run with the modified priority.
Example 1: Running a command with lower priority
nice -n 10 sleep 100
This command runs sleep 100
with a nice value of 10, meaning it will have lower CPU priority.
Example 2: Running a command with higher priority
sudo nice -n -10 ./myscript.sh
This command runs myscript.sh
with a nice value of -10 (higher priority), but requires sudo
because negative nice values require superuser privileges.
2. renice
Command:
The renice
command is used to change the priority of an already running process. You can use it to adjust the nice value of a process after it has started.
Syntax:
renice -n <priority> -p <pid>
<priority>
: The new nice value you want to assign (range from -20 to 19).<pid>
: The process ID (PID) of the process whose priority you want to change.
Example 1: Changing the priority of a running process
renice -n 5 -p 1234
This command changes the nice value of the process with PID 1234 to 5, reducing its priority.
Example 2: Increasing the priority of a running process (requires sudo)
sudo renice -n -5 -p 1234
This command increases the priority of the process with PID 1234 by setting its nice value to -5.
Notes:
- Positive nice values (from 1 to 19) lower the priority, which is useful when you want to prevent a process from consuming too much CPU time.
- Negative nice values (from -1 to -20) increase the priority, which is useful for giving critical processes higher CPU priority.
- Root (superuser) privileges are required to set negative nice values (higher priority) for processes.