Linux/Bash

Linux Process Priorities With Nice and Ionice for Background Jobs

4 min read by DebuggedIt

Quick answer

The nice command lowers a process's CPU scheduling priority, and ionice lowers its disk I/O priority, so background jobs such as backups and batch processing...

The nice command lowers a process's CPU scheduling priority, and ionice lowers its disk I/O priority, so background jobs such as backups and batch processing interfere less with latency-sensitive services. Both are hints to the kernel scheduler, not hard limits. For guaranteed resource boundaries on production servers, cgroup-based controls through systemd are more reliable.

How Does nice Affect CPU Scheduling?

Every process has a niceness value, typically from -20 for the highest priority to 19 for the lowest. The default is 0. A higher niceness means the process is more willing to yield CPU time to others. Regular users can only increase niceness, while lowering it below the default requires elevated privileges.

nice -n 10 ./nightly-report.sh
renice -n 15 -p 4321
ps -o pid,ni,cmd -p 4321

Niceness matters only when processes compete for CPU. On an idle machine, a niced job still gets all the CPU it wants. When the machine is busy, the scheduler gives lower-priority processes a smaller share, but not zero, so a heavily niced job can still consume meaningful CPU over time.

The exact weighting depends on the scheduler in your kernel version, so consult the kernel and man page documentation if you need precise numbers.

How Does ionice Affect Disk I/O?

CPU is often not the real bottleneck for backups, log processing, or database dumps. Disk throughput is. The ionice command sets the I/O scheduling class and priority of a process.

  • Idle class: the process gets disk time only when no one else needs it.
  • Best-effort class: the default, with priority levels from 0 to 7 within the class.
  • Realtime class: highest priority, requires privileges, and can starve other processes, so use it with great care.
ionice -c 3 tar czf /backup/site.tar.gz /var/www
ionice -c 2 -n 7 rsync -a /data/ /mnt/archive/
ionice -p 4321

An important caveat is that I/O priorities depend on the I/O scheduler in use. Some modern schedulers, and many setups with fast NVMe devices, handle priorities differently or ignore them. Test the effect on your system rather than assuming it works.

What Are the Limitations of nice and ionice?

Both tools are advisory. They change relative priority, but they do not cap resource usage. A low-priority job can still fill the page cache, saturate memory bandwidth, or generate bursts of writes that hurt latency for other services.

  • Memory is not covered. A niced job can still cause swapping or trigger the OOM killer.
  • Network usage is unaffected.
  • Write-heavy jobs can flood the cache and later cause stalls when dirty pages are flushed.
  • Priorities do not stop a job from holding locks that block other processes.

Containers and virtual machines add another wrinkle. Priorities inside a container compete only with processes in the same context, and the host's limits determine what the container sees.

The lock problem is worth remembering. If a low-priority job holds a database lock or a file lock and then gets little CPU, every higher-priority process waiting on that lock is effectively slowed down too. This situation, known as priority inversion, is one reason heavy background work should be designed to hold locks briefly.

When Should You Use cgroups or systemd Instead?

If a background task must not disturb production traffic, enforce limits with cgroups. Systemd exposes these as unit properties, making them easy to apply to services and one-off commands.

systemd-run --scope -p CPUQuota=25% -p IOWeight=20 -p MemoryMax=512M ./nightly-report.sh

CPUQuota caps CPU time, IOWeight sets relative disk weight, and MemoryMax limits memory. Available properties and cgroup version behavior differ between systems, so check the systemd documentation for your distribution.

A good rule is to use nice and ionice for quick, informal prioritization, such as running a one-time compression job, and use systemd resource controls for scheduled or recurring jobs that must stay within defined limits.

Frequently Asked Questions

Does nice make a process run slower?

Only when CPU is contended. On an idle system it runs at full speed. Under load it receives a smaller share than higher-priority processes.

Can I change priority of a running process?

Yes. Use renice for CPU niceness and ionice with the -p option for I/O priority. Raising priority above the default requires privileges.

Is ionice effective on SSDs and NVMe drives?

It depends on the I/O scheduler. Many fast devices use schedulers that ignore or weaken priorities, so measure before relying on it.

Should cron jobs use nice and ionice?

Often yes, for heavy maintenance tasks. For strict guarantees, run them as systemd units with resource limits.

Summary

Use nice and ionice to make background jobs polite, but remember they are hints. For firm limits on CPU, I/O, and memory, rely on cgroups through systemd. Verify scheduler behavior in the kernel and systemd documentation for your version.