Skip to main content

Command Palette

Search for a command to run...

nice cmd

Updated
3 min readView as Markdown

🧠 What is Process Priority in Linux?

The CPU cannot run all processes at the same time. So Linux uses a scheduler that decides which process gets CPU first.

Each process has a priority value called the “nice value.”

Nice Value Range Nice Value Priority -20 Highest priority 0 Default priority 19 Lowest priority

Important idea:

Lower nice value → Higher priority Higher nice value → Lower priority

The name “nice” comes from the idea that a process can be “nice” to other processes by using less CPU.

⚙️ nice Command

nice is used to start a process with a specific priority.

Syntax:

nice -n [nice_value] command

Example:

nice -n 10 command

This runs the command with lower priority, meaning it will not consume CPU aggressively.

🔹 Example 1 — Running a Heavy Script

Suppose you have a backup script that consumes CPU.

Run normally:

./backup.sh

This may slow down the system.

Better approach:

nice -n 10 ./backup.sh

Now the backup will run with lower priority so the system remains responsive.

🔹 Example 2 — Compressing Large Files

Compression uses a lot of CPU.

Example:

nice -n 15 tar -czf backup.tar.gz folder/

Meaning:

tar process will run with low priority other programs will get CPU first

Useful on production servers.

🔹 Example 3 — Running a Python Script

Example:

nice -n 5 python data_processing.py

This ensures the data processing job doesn't disturb other services.

🔹 Example 4 — Running a Build Process

Compiling code can consume CPU.

Example:

nice -n 10 make build

Now the build process will be less aggressive on CPU usage.

🔍 Checking Process Priority

Use top or ps.

Using top top

Look at the NI column.

Example:

PID USER PR NI VIRT RES CPU 3456 lav 20 10 400M 20M 5%

NI = 10 → nice value.

Using ps ps -o pid,ni,comm

Example output:

PID NI COMMAND 1234 0 bash 2345 10 python 🔧 Changing Priority of Running Process (renice)

If a process is already running, use renice.

Example:

renice 10 -p 1234

This changes the priority of process PID 1234.

🚀 DevOps / Developer Use Cases 1️⃣ Running background jobs safely

Example:

Database backup Log processing Data analytics

Run with lower priority:

nice -n 15 backup.sh 2️⃣ Prevent heavy jobs from slowing servers

Example:

Video processing AI training Large builds

Run with:

nice -n 10 python train_model.py 3️⃣ Production server stability

Example:

On a server running:

Nginx Docker Database

You run:

nice -n 15 tar -czf logs.tar.gz /var/log

Now web traffic is not affected.

📊 Example Scenario

Server tasks running:

  1. Nginx web server

  2. MySQL database

  3. Backup script

Without nice:

backup.sh consumes CPU website becomes slow

With nice:

nice -n 10 ./backup.sh

Result:

web server stays responsive backup runs slowly in background 📌 Summary Command Purpose nice start process with specific priority renice change priority of running process top view process priority ps show nice values

Nice value rule:

-20 → highest priority 0 → default 19 → lowest priority

💡 Important DevOps Interview Tip

They might ask:

Why would you use nice in production?

Answer:

To run CPU-intensive background tasks with lower priority so that critical services like web servers, databases, and containers remain responsive.