Nice

On this page 7

Nice is the per-process scheduling hint in Linux that tells the kernel how generously to share CPU time with a process, and it is also the name of the command that starts a program with a chosen value. The nice value ranges from -20 to 19, with 0 as the default: a higher value makes a process “nicer” to others by yielding CPU time, while a lower value makes it more demanding.

In Linux processes, nice applies to the normal scheduling classes only. It shapes how the CPU is divided among runnable tasks that compete for the same cores, but it does not reserve CPU time, it does not affect memory, and it has no effect on an idle system where every task already gets the CPU it asks for.

How the nice value influences the scheduler

The Linux scheduler translates each nice value into a load weight. Under the Completely Fair Scheduler, and under the EEVDF scheduler that replaced it in kernel 6.6, a task with nice 0 has a weight of 1024, and each step of nice changes the weight by roughly a factor of 1.25. This means one step of nice shifts about 10 percent of CPU time between two competing tasks.

The table shows the weights for selected nice values and the approximate CPU share when such a task competes with one nice-0 task on a single core.

Nice valueKernel weightApproximate CPU share
-2088761About 99 percent
-109548About 90 percent
-53121About 75 percent
0102450 percent
5335About 25 percent
10110About 10 percent
1915About 1.5 percent
Nice values, kernel weights and CPU share against one nice-0 task

Because the effect is relative rather than absolute, the same nice value produces different results depending on what else runs. A nice-19 batch job on an otherwise idle machine still receives nearly the full CPU, and it only yields when other runnable tasks compete for the same core.

The nice and renice commands

The nice command starts a new program with an adjusted value, while renice changes the value of a process that is already running. Both operate on the same kernel attribute, which the ps -o ni and top columns display as NI.

CommandPurposeExample
niceStart a command with an offsetnice -n 10 tar czf backup.tgz /srv
reniceChange a running processrenice -n 5 -p 4321
renice -uChange all processes of a userrenice -n 10 -u builder
ps / topDisplay the current valueps -o pid,ni,cmd -p 4321
systemd Nice=Set the value for a service unitNice=10 in the [Service] section
Commands that work with nice values

Without the -n option, nice adds an increment of 10. Values outside the range are clamped, so nice -n 50 simply results in nice 19. Child processes inherit the nice value of their parent, so starting a shell with nice 10 makes every command run from it equally polite.

Permissions and limits on nice values

Any user may raise the nice value of their own processes, making them less demanding. Lowering the value below its current level, and therefore claiming more CPU, requires the CAP_SYS_NICE capability, which normally means root. This asymmetry prevents ordinary users from starving other users on a shared system.

The resource limit RLIMIT_NICE relaxes this rule in a controlled way. Configured through /etc/security/limits.conf with the nice item, or through LimitNICE= in a systemd unit, it lets a specific user or service lower its value down to a defined floor, which is common for audio and media workloads.

Nice is only one of several tools that shape CPU usage, and it is the weakest and simplest of them. Process priority as a broader concept, CPU affinity and cgroups solve different problems, and choosing the right one avoids frustration.

ControlWhat it changesScopeStrength
NiceRelative weight in the fair schedulerOne process or threadSoft, only under contention
Real-time priorityPolicy SCHED_FIFO or SCHED_RR, levels 1-99One process or threadStrict, preempts all normal tasks
CPU affinityWhich cores a task may run onOne process or threadHard placement, no share control
Cgroup cpu.weightRelative weight of a group of tasksA whole service or containerSoft, applies across groups
Cgroup cpu.maxAbsolute quota per periodA whole service or containerHard ceiling even on idle CPU
Nice and neighboring CPU controls

An important interaction is the autogroup feature. On many desktop distributions the kernel groups tasks by session, and nice values then compete only within the same session. A nice-19 job started in one terminal can therefore still receive half of the CPU against a nice-0 job in another session, which surprises administrators who expect a global effect.

When to use nice and when not to

Nice works best for long-running, CPU-bound background work that should not disturb interactive use or latency-sensitive services. It does not help when the bottleneck is disk or memory, because the I/O scheduler and the memory manager largely ignore it.

SituationRecommended controlReason
Nightly compression or backupsnice 10-19 plus ioniceYields CPU and disk to live services
Compiling on a developer workstationnice 10Keeps the desktop responsive
Limiting a container to two corescgroup cpu.maxNice cannot set an absolute cap
Low-latency audio processingReal-time priorityNice cannot guarantee deadlines
Database competing with batch jobscgroup cpu.weight per serviceApplies to all threads of each service
Choosing a control for typical situations
  • Pair nice with ionice -c3 when a job is heavy on disk as well as CPU.
  • Do not lower nice values of daemons to fix slowness without measuring; it often hides a capacity problem.
  • Remember that nice is per thread in Linux, so renice on one thread ID does not change sibling threads.
  • Verify the effect under real contention with top or htop rather than on an idle test machine.

Put simply

Nice is a politeness setting for programs. When several programs want the processor at the same moment, a polite program with a high nice value steps aside and lets the others go first, while a pushy one with a low value claims a bigger turn.

If nobody else is waiting, even the most polite program gets the whole processor. Nice only matters when there is competition, and only an administrator may make a program pushier than it was.

In practice

A web hosting server with eight cores runs customer websites during the day and a nightly job that compresses about 200 GB of log files. When the job ran at the default nice 0, page response times rose sharply during the compression window, because the job occupied several cores at full load.

The administrator changes the cron entry to nice -n 19 ionice -c3 /usr/local/bin/compress-logs. The job now uses the CPU only when web requests leave cores free, and it still finishes before morning because the server is lightly loaded at night. Response times during the window return to their normal level, and the change costs nothing beyond a slightly longer job duration.