This post was originally published on go2linux.org. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements. It merges several short posts about nice and renice into one.
You are working normally, a cron job or a long compilation starts, and everything else slows down. You can avoid that by telling the kernel which processes matter less. nice starts a command with a different priority, and renice changes the priority of a process that is already running.
Niceness
Every process has a niceness value from -20 to 19:
- -20 is the highest priority (the least "nice" to other processes).
- 0 is the default.
- 19 is the lowest priority.
Niceness only matters when processes compete for the CPU. On an idle machine a process with niceness 19 runs at full speed. When the CPU is busy, a higher niceness means a smaller share of it. It is a weight, not a limit: a niced process is never stopped completely, and it never has a cap on the CPU it may use.
Start a command with nice
Without options nice adds 10 to the niceness:
nice make all
Choose the value with -n:
nice -n 19 tar czf backup.tar.gz /home
The number is added to the niceness of the shell that starts the command, which is normally 0. Run nice with no arguments to print the current value:
nice
0
A negative value raises the priority, and only root may do that:
sudo nice -n -5 ./realtime-encoder
Change a running process with renice
Find the process ID with pgrep, ps or top, then:
renice -n 10 -p 1234
1234 (process ID) old priority 0, new priority 10
Unlike nice, the number given to renice is the new niceness, not an increment.
Several processes at once:
renice -n 10 -p 1234 5678
Every process of a program, using pgrep to get the IDs:
renice -n 15 -p $(pgrep ffmpeg)
Every process owned by a user:
sudo renice -n 5 -u backup
Every process in a process group, with -g:
renice -n 10 -g 4321
Old tutorials, including the original version of this post, use the form renice +1 987. It still works, but -n and -p are clearer.
What a normal user may do
- Raise the niceness (lower the priority) of their own processes.
- Nothing else.
A normal user cannot give a negative niceness, cannot touch the processes of other users, and cannot lower the niceness again after raising it, not even back to the original value:
renice -n 10 -p 1234 # works
renice -n 0 -p 1234 # Permission denied
Use sudo for that. An administrator can allow a user or group to go down to a given niceness with the nice item in /etc/security/limits.conf.
See the niceness of a process
With ps:
ps -o pid,ni,comm -p 1234
PID NI COMMAND
1234 10 tar
In top and htop it is the NI column. Both can change it interactively:
- In
top, pressr, type the PID and then the new value. - In htop, highlight the process with the arrow keys and press
F8to raise the niceness (lower priority) orF7to reduce it (higher priority, root only).
Disk priority: ionice
nice only affects CPU scheduling. A backup at niceness 19 can still make the machine sluggish by saturating the disk. ionice sets the I/O priority:
ionice -c 3 tar czf backup.tar.gz /home
Class 3 is "idle": the process gets disk time only when nobody else wants it. Combine both for background jobs:
nice -n 19 ionice -c 3 tar czf backup.tar.gz /home
Change a running process with -p:
ionice -c 3 -p 1234
ionice is only honored by I/O schedulers that implement priorities, such as BFQ. With none or mq-deadline, common on NVMe drives, it has little or no effect. Check the scheduler in use with:
cat /sys/block/sda/queue/scheduler
Practical examples
A nightly backup from cron that does not disturb the rest of the system:
0 3 * * * nice -n 19 ionice -c 3 /usr/local/bin/backup.sh
Compile using all cores but stay out of the way:
nice -n 15 make -j"$(nproc)"
Calm down a process that is already hogging the CPU:
renice -n 19 -p $(pgrep -f "convert-videos.sh")
For a systemd service, set it in the unit file instead of wrapping the command:
[Service]
Nice=10
IOSchedulingClass=idle
See also
man nice, man renice and man ionice — full reference.