Linux/Unix: Process Scheduling β nice, renice, chrt Explained with Examples
Introduction β Why Learn Scheduling Commands in Linux?
Linux is a multi-tasking operating system, where multiple processes run concurrently. Sometimes, you need to prioritize or deprioritize processes for optimal performance. Thatβs where nice, renice, and chrt come inβthey control the CPU scheduling priority of processes.
In this guide, youβll learn:
- How Linux handles process priorities
- Difference between nice values and real-time priorities
- How to schedule tasks using
nice, modify them usingrenice, and apply real-time policies usingchrt
Understanding Scheduling in Linux
- Nice value: Determines priority for non-real-time processes (range:
-20[highest] to19[lowest], default is0) - Real-time priority: Managed by
chrt, works with FIFO and RR (Round Robin) policies
| Tool | Affects | Range / Modes | Privilege Required |
|---|---|---|---|
nice | New processes | -20 to 19 (default 0) | Yes (for -ve) |
renice | Running processes | -20 to 19 | Yes (for others) |
chrt | Real-time tasks | FIFO, RR, Other | Yes (root needed) |
nice β Launch Process with Custom Priority
Syntax:
nice -n [value] command
Description:
Runs a command with a custom nice value. Higher value = lower priority.
Example 1: Run with lower priority
nice -n 10 long_running_task.sh
Output: (No output unless the script prints something)
Example 2: Run with higher priority (requires root)
sudo nice -n -5 myscript.sh
renice β Change Priority of a Running Process
Syntax:
renice [nice_value] -p [PID]
Description:
Changes the nice value of an already running process.
Example:
renice 5 -p 1234
Output:
1234 (process ID) old priority 0, new priority 5
Change priority for all userβs processes:
renice 10 -u john
chrt β Set or View Real-Time Scheduling
Syntax:
chrt [options] priority command
Description:
Assigns a real-time scheduling policy (FIFO, RR, OTHER) and priority to a process.
Example 1: Run with FIFO policy
sudo chrt -f 50 ./critical_task
Output: (Runs in real-time FIFO mode at priority 50)
Example 2: Check current scheduling policy
chrt -p 1234
Output:
pid 1234's current scheduling policy: SCHED_OTHER
pid 1234's current scheduling priority: 0
Scheduling Use Cases
| Task | Use This Tool | Example |
|---|---|---|
| Lower priority for backup | nice | nice -n 15 rsync ... |
| Raise priority for compiler | renice | renice -5 -p 3231 |
| Real-time video rendering | chrt | chrt -f 80 ./render |
| Balance multi-user CPU load | renice | renice 10 -u username |
Summary β Recap & Next Steps
Linux lets you tune process scheduling to manage system performance and responsiveness. With nice, renice, and chrt, you can control which tasks get CPU time and whenβensuring critical processes run smoothly.
Key Takeaways:
nicestarts a command with a custom priority.renicechanges priority of already running processes.chrtassigns real-time priorities using FIFO/RR policies.- Lower nice values = higher priority;
chrtoffers stricter scheduling.
FAQs
Whatβs the default nice value in Linux?
Itβs 0.
Whatβs the lowest and highest nice value possible?
Highest priority = -20, Lowest priority = 19.
Can any user lower the nice value (raise priority)?
No, only root can assign negative nice values.
What is the difference between nice and chrt?
nice changes user-space CPU priority, while chrt controls real-time scheduling policies.
How do I check the priority and scheduling policy of a process?
Use:
chrt -p <PID>
Share Now :
