Docs / Linux Basics / Linux Process Priorities: nice and renice Explained

Linux Process Priorities: nice and renice Explained

By Admin · Mar 15, 2026 · Updated Apr 23, 2026 · 176 views · 3 min read

Linux process scheduling determines how CPU time is distributed among running processes. The nice and renice commands let you adjust process priorities to ensure critical workloads get the CPU time they need while background tasks yield gracefully.

Understanding Nice Values

# Nice values range from -20 (highest priority) to 19 (lowest priority)
# Default nice value for new processes: 0
# Only root can set negative nice values (higher priority)

# View nice values of running processes
ps -eo pid,ni,comm --sort=-ni | head -20

# The NI column shows the nice value:
#  PID  NI COMMAND
# 1234   0 nginx
# 5678  10 backup-script
# 9012 -10 mysql

Starting a Process with a Specific Priority

# Start a CPU-intensive backup at low priority
nice -n 15 tar czf /backup/full-backup.tar.gz /var/www

# Start a database import at high priority (requires root)
sudo nice -n -10 mysql mydb < large-dump.sql

# Default nice command adds 10 to priority
nice tar czf backup.tar.gz /data   # Runs at nice 10

Changing Priority of a Running Process

# Renice by PID
sudo renice -n 15 -p 1234

# Renice all processes by a user
sudo renice -n 10 -u www-data

# Renice all processes in a process group
sudo renice -n 5 -g 5678

# Practical example: Lower priority of a runaway process
ps aux | grep "cpu-hungry-script"
sudo renice -n 19 -p $(pgrep cpu-hungry-script)

Using ionice for I/O Priority

# ionice controls I/O scheduling priority (disk access)
# Classes: 0=none, 1=realtime, 2=best-effort, 3=idle

# Run backup with idle I/O priority (only uses disk when idle)
ionice -c 3 tar czf backup.tar.gz /var/www

# Run database maintenance at best-effort with low priority
ionice -c 2 -n 7 mysqlcheck --all-databases --optimize

# Combine nice and ionice for minimal system impact
nice -n 19 ionice -c 3 rsync -a /source/ /destination/

# Check I/O priority of a process
ionice -p $(pgrep mysql)

systemd Service Priorities

# Set nice value for a systemd service
sudo systemctl edit myapp.service

# Add these lines:
[Service]
Nice=-5
IOSchedulingClass=best-effort
IOSchedulingPriority=4
CPUWeight=150

# Reload and restart
sudo systemctl daemon-reload
sudo systemctl restart myapp

Practical Priority Strategies

Web Server Priority

# Database (high priority): nice -5 to -10
# Web server (normal): nice 0
# Background workers: nice 5-10
# Backups: nice 15-19
# Log rotation: nice 19

# Example: Ensure MySQL always gets CPU time
sudo renice -n -5 -p $(pgrep mysqld)

cgroups for More Control

# For fine-grained resource control, use cgroups via systemd
# Limit a service to 50% CPU
sudo systemctl edit myworker.service
# [Service]
# CPUQuota=50%
# MemoryMax=2G

sudo systemctl daemon-reload
sudo systemctl restart myworker

Monitoring Priority Impact

# Watch how nice values affect CPU distribution
# Start two CPU-intensive processes with different priorities
nice -n 19 sha256sum /dev/urandom &
nice -n 0 sha256sum /dev/urandom &

# Watch in htop — the nice 0 process gets more CPU time
htop

# Clean up
killall sha256sum

Was this article helpful?