nice and renice Basics - Controlling Process Priority
What Are nice and renice?
nice and renice are Linux commands for controlling a process's CPU scheduling priority. Processes with higher priority receive more CPU time from the scheduler.
Key Takeaways
- Want to run heavy processing without impacting other services? → Use
niceto lower priority at launch - Running process consuming too much CPU? → Use
reniceto lower its priority dynamically - Regular users can only lower priority — raising it requires root privileges
What Is a Nice Value?
A nice value is an integer from -20 to 19. Lower values mean higher priority.
| Nice Value | Priority | Typical Use |
|---|---|---|
| -20 | Highest | Real-time processing (root only) |
| 0 | Normal (default) | Typical processes |
| 10 | Lower | Background batch jobs |
| 19 | Lowest | Run only when system is idle |
Despite the name, a process with a higher nice value (like 19) is "nicer" to other processes — it voluntarily takes less CPU. Counter-intuitive, but that's the convention.
nice - Set Priority at Launch
Syntax
nice -n <nice_value> <command>
The -n option specifies how much to adjust the nice value relative to the current process's nice value. Omitting -n applies the default +10 increment (lower priority).
Examples
# Run backup with lowered priority (nice=10) nice -n 10 tar czf /backup/home.tar.gz /home/ # Run at minimum priority (nice=19) nice -n 19 ./long-batch-job.sh # Omit -n flag (defaults to +10) nice ./heavy-script.sh
Check the current nice value
nice
0
Running nice with no arguments prints the current shell's nice value.
renice - Change Priority of a Running Process
Syntax
renice -n <nice_value> -p <PID> renice -n <nice_value> -u <username> renice -n <nice_value> -g <groupname>
Change by PID
# Set PID 1234 to nice value 15 renice -n 15 -p 1234
1234 (process ID) old priority 0, new priority 15
Change all processes for a user
# Set all processes owned by "worker" to nice value 10 renice -n 10 -u worker
Combine with pgrep to target a process by name:
renice -n 19 -p $(pgrep heavy-job)
How to Check Nice Values
Using top
The NI column in top shows the nice value. The PR column shows the kernel's computed scheduling priority (PR = 20 + NI).
top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1234 user 30 10 102400 12000 8000 R 15.0 0.2 0:05.23 tar
Using ps
ps -eo pid,ni,comm
PID NI COMMAND
1 0 systemd
1234 10 tar
5678 0 bash
Permission Rules
Priority adjustments are subject to the following restrictions:
| Action | Regular User | Root |
|---|---|---|
| Lower priority (increase nice value) | Yes | Yes |
| Raise priority (decrease nice value) | No | Yes |
| Modify another user's processes | No | Yes |
Attempting to raise priority as a regular user fails:
renice -n -5 -p 1234
renice: failed to set priority for 1234 (process ID): Permission denied
Practical Patterns
# Launch nightly batch at low priority to avoid impacting services nice -n 15 /opt/scripts/nightly-backup.sh & # A running job is too heavy — deprioritize it without stopping renice -n 19 -p $(pgrep heavy-job) # Compress large log file in the background without slowing other work nice -n 10 gzip /var/log/large.log &
Avoid These Mistakes
- Applying
nice -n -20to a production service as root — it can starve other services of CPU - Relying on nice alone to cap CPU usage — use
cgroupsorcpulimitfor hard limits