How to Configure Linux Process Priorities using the nice and renice Commands

The Linux Scheduler

In a standard Linux environment, the kernel’s scheduler is responsible for dividing the CPU’s time among hundreds of running processes. By default, the scheduler treats almost all user-space processes equally. However, in a production environment, not all processes are equal. If a user kicks off a massive, CPU-intensive video rendering job (like ffmpeg) at the same time a critical web server (like nginx) is trying to serve traffic, the web server will suffer massive latency as the two applications fight for CPU cycles.

To solve this, system administrators use “Niceness.” In Linux, the Nice value determines how “nice” a process is to other processes. A process with a high nice value is very polite; it steps back and allows other processes to use the CPU first. A process with a low nice value is aggressive; it demands CPU time immediately.

The nice scale ranges from -20 (Highest Priority, most aggressive) to +19 (Lowest Priority, most polite). The default value for a new process is 0.

Launching a Process with nice

If you know an application is going to be heavily resource-intensive and you want it to run entirely in the background without affecting the server’s primary duties, you can launch it with a polite nice value using the nice command.

For example, to run a massive data backup script (backup.sh) with the lowest possible priority (+19):

nice -n 19 ./backup.sh

The backup script will now execute. If the CPU is sitting idle, the backup script will utilize 100% of it. But the exact millisecond a web request comes in, the backup script will instantly pause, allowing the web server (which is running at the default priority of 0) to process the request instantly.

Altering Priority with renice

If a process is already running, you cannot use nice; you must use renice to change its active priority on the fly.

First, identify the Process ID (PID) of the application. Let’s assume a runaway Python script (PID 4455) is currently consuming 100% of the CPU and causing SSH to lag.

To forcefully lower its priority to +10, execute:

renice -n 10 -p 4455

The terminal will output: 4455 (process ID) old priority 0, new priority 10. The SSH daemon will instantly become responsive again.

The Privilege of Aggression

While any standard user can use renice to make their own processes more polite (e.g., going from 0 to +10), a standard user is mathematically prohibited from making a process more aggressive (e.g., going from 0 to -10, or even from +10 back to 0).

To assign a negative nice value (demanding high priority), you must possess root privileges.

To aggressively prioritize the Nginx web server (PID 1024) to ensure it never waits for CPU cycles, run:

sudo renice -n -15 -p 1024

The Risk of Starvation

You must exercise extreme caution when assigning negative nice values, particularly -20. If you assign -20 to a poorly written application that gets stuck in an infinite loop, it will aggressively monopolize the CPU, starving all other processes. The SSH daemon (running at 0) will be unable to get any CPU time, meaning you won’t even be able to log in to kill the rogue process, forcing a hard physical reboot of the server.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.