Linux nice Command: Setting Priority, Changing It with renice, and Measuring the CPU Share Under Contention

The Linux nice command sets a program's nice value (-20 to 19), which decides how much CPU it gives up to other processes when it runs. Measurements on an AlmaLinux 9 server, with two tasks pinned to a single CPU core and the nice value changed between runs, showed that the CPU was shared out only when the tasks competed for it, and the nice 19 task used just 1.4%.
Processes that are already running are changed with renice, and ionice handles giving way on the disk side. For all three commands, the output shown is exactly what they printed when run as root and as a regular user.
1️⃣ Linux nice Command: Value Range and the CPU Share Under Contention
The nice value is a number that sets how much a process gives way when CPU time is divided, and it can be set from -20 to 19. Programs normally run at 0; -20 is served first, and 19 gives way the most.
It is easy to assume a bigger number gets served first, but nice values work the other way round. The Linux scheduler assigns a weight to each nice value and divides CPU time in that ratio: nice 0 is 1024, 10 is 110 and 19 is 15.
Giving way only happens when tasks compete for the CPU. If there is an idle CPU, even a nice 19 task runs without waiting. It is like priority seats on a bus: when they are free anyone can sit, and people give up their seats only when the bus is crowded.
The weight table is the [sched_prio_to_weight] array in the Linux kernel source [kernel/sched/core.c], and it is designed so that each one-step rise in the nice value cuts CPU time by about 10%.
2️⃣ Basic nice Usage: +10 When the Number Is Omitted, and Out-of-Range Values
Put a command after nice and that command runs with a changed nice value; type nice on its own and it prints the current shell's nice value.
nice0In the examples below, the bash that gets started prints its own nice value with [ps -o ni=]. Run without -n, 10 is added to the current value; with -n 5, 5 is added.
nice bash -c 'ps -o ni= -p $$' 10nice -n 5 bash -c 'ps -o ni= -p $$' 5Values outside the range are clamped to the limit without an error, so -n 50 runs at 19.
nice -n 50 bash -c 'ps -o ni= -p $$' 19Only root can move a process ahead with a negative value. A regular user gets the [cannot set niceness] warning, and the command still runs at nice 0.
# as a regular user
nice -n -5 bash -c 'ps -o ni= -p $$'nice: cannot set niceness: Permission denied
0# as root
nice -n -5 bash -c 'ps -o ni= -p $$' -5Child processes started by a command run under nice also run with the same nice value.
nice -n 7 bash -c 'sleep 1 & ps -o ni= -p $!' 7So if you run a script with nice, every command inside it gives way too.
3️⃣ renice: Changing the nice Value of a Running Process
nice only sets the value at launch, so for a process that is already running you use renice. First check the PID and the current value in top's NI column or with ps.
ps -o pid,ni,cmd -p 201643 PID NI CMD
201643 5 sleep 600renice -n 10 -p 201643201643 (process ID) old priority 5, new priority 10As the output shows, renice -n does not add to the current value but sets it to the value you give: the result is 10, not 5 + 10 = 15. It is not like pressing ▲ on an air-conditioner remote to go up one step, but like setting the target temperature to 24 degrees.
The official util-linux documentation also describes -n as an absolute value; adding is handled by the [--relative] option in newer versions. util-linux 2.37.4, the version tested, did not have this option.
A regular user can only raise the value of their own processes and cannot lower it again once it has been raised.
# as a regular user
renice -n 10 -p 201649201649 (process ID) old priority 0, new priority 10renice -n 5 -p 201649renice: failed to set priority for 201649 (process ID): Permission deniedroot can lower it as well; in the same test, it changed 10 straight to 3.
🔲 nice · renice Simulator
In the simulator below, logged in as a regular user, you can type nice and renice yourself and watch the nice value change.
4️⃣ Measuring the CPU Share Under Contention: nice 0 vs 0·10·19
Raising the nice value does not always slow the task down. Its share shrinks only when it competes with another task for the same CPU.
Two tasks were pinned to a single CPU core (taskset), and the script below, which counts up one number at a time for 5 seconds, was run in both at the same time. The higher the count, the more CPU time the task used.
#!/bin/bash
end=$((SECONDS+5)); n=0
while [ $SECONDS -lt $end ]; do n=$((n+1)); done
echo $nThe script increases n by 1 until 5 seconds have passed, then prints the final value.
Two nice 0 tasks split it in half, while nice 10 used about 10% and nice 19 just 1.4%. This matches the ratios calculated from the kernel weights (1024 : 110 gives 90.3 : 9.7, and 1024 : 15 gives 98.6 : 1.4).
Run alone on the same core, the nice 19 task counted roughly 650,000 to 1,140,000 times in 5 seconds. With nobody to give way to, even nice 19 uses the CPU fully; the server load (load average) was 3 to 5 during the test.
5️⃣ ionice: Giving Way on Disk I/O and Checking the Scheduler
nice only decides the CPU share, so for tasks that read and write the disk for a long time, such as backups, use ionice to give way on disk order as well. Run with -c3 (idle), a task is served only when no other program is using the disk.
ionice -c3 bash -c 'ionice -p $$'idleionice -p $$none: prio 0A process with no class set is shown as [none]; according to the kernel documentation, it is treated as best-effort, with its priority calculated from the CPU nice value.
ionice classes only take effect when the disk scheduler supports them, and the kernel documentation lists bfq and mq-deadline as supporting schedulers. The one shown in brackets is the scheduler currently in use.
cat /sys/block/nvme0n1/queue/scheduler[none] mq-deadline kyber bfqThe NVMe disk on the test server uses [none], so according to the official documentation ionice may have no effect there; the effect was not measured separately. Check the disk name with lsblk.
6️⃣ Where to Use nice: Backups · Cron · wp-cli
Add nice to jobs that take a long time but are not urgent. Backup compression uses both CPU and disk, so set nice and ionice together.
nice -n 19 ionice -c3 tar czf backup.tgz /home/exampleIf a job that is already running slows the server down, find its PID in top and raise its value with renice; for cron jobs that run every day, put nice in front of the command.
0 3 * * * nice -n 10 /usr/local/bin/backup.shOn a WordPress server, if you run big wp-cli jobs such as search-replace or image regeneration with nice -n 10, php-fpm, which handles visitor requests, gets the CPU first when it is busy.
nice -n 10 wp media regenerate --yesOn the other hand, do not raise the nice value of php-fpm or the web server itself. If the processes that answer visitors give way, pages respond more slowly when the CPU is busy.
🔢 FAQ & Recommended Content









ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.
