Back to Blog
Lesson 46 of the Linux: Linux Command Line for Developers course
LinuxSeptember 3, 20264 min read

Advanced Process Priority: Managing CPU Scheduling with Nice and Renice

Learn how to manage process priority in Linux using nice and renice. Master CPU scheduling to ensure your critical services always get the resources they need.

linuxcommand-linesystem-administrationperformanceprocessesnice
A detailed close-up of a CPU microchip showcasing its intricate gold pins and technology.

Previously in this course, we covered managing process lifecycles and using real-time monitoring with top. While those lessons focused on observing and terminating processes, this lesson adds the ability to actively influence how the Linux kernel schedules CPU time for your applications.

Understanding Process Priority and "Niceness"

In a multitasking operating system, the CPU is shared among dozens or hundreds of processes. The Linux kernel uses a scheduler to decide which process gets CPU time and for how long. By default, processes are given an equal share, but as a system administrator, you often need to protect critical services (like your web server) from being starved by resource-heavy background tasks (like backups or log indexing).

Linux manages this via a "niceness" value, which ranges from -20 to 19:

  • -20 (Most favorable): The process is "greedy." The kernel will prioritize it above almost everything else.
  • 0 (Default): The standard priority for most applications.
  • 19 (Least favorable): The process is "polite." It will only run when the CPU has spare cycles.

Think of it as a social contract: a "nicer" process waits for others to finish before taking its turn at the CPU.

Setting Priority at Launch with nice

The nice command is used when starting a new process. If you are running a heavy task, such as a log rotation script or a data cleanup job, you should lower its priority so it doesn't impact your web server’s response time.

To start a command with a specific priority, use the -n flag:

Bash
# Start a backup script with a 'nice' value of 10
nice -n 10 ./backup_logs.sh

If you don't specify a value, nice defaults to 10. Note that you generally cannot lower the niceness value (make it more favorable, e.g., -5) unless you are the root user, as this prevents regular users from hogging system resources.

Adjusting Running Processes with renice

Often, you won't know a process will consume excessive resources until it is already running. The renice command allows you to change the priority of a process that is already active without stopping it.

First, identify the Process ID (PID) using top or ps. Then, update it:

Bash
# Change the priority of PID 1234 to 15
renice -n 15 -p 1234

You can also target all processes owned by a specific user:

Bash
# Make all processes owned by 'www-data' much nicer (priority 19)
renice -n 19 -u www-data

Hands-on Exercise: Throttling a Background Task

In our ongoing project, we want to ensure that our log rotation script doesn't interfere with the Nginx web server.

  1. Open your terminal and create a dummy "heavy" process: yes > /dev/null & (This command generates a continuous stream of data and consumes 100% of a CPU core).
  2. Find its PID using ps aux | grep yes.
  3. Observe its CPU usage in top.
  4. Lower its priority to make it "nice": renice -n 19 -p <PID>.
  5. Check top again; notice how the CPU usage drops as other processes are given preference.
  6. Terminate the process when finished: kill <PID>.

Common Pitfalls

  • Misunderstanding the Scale: It is intuitive to think a higher number means "higher priority." Remember: Higher Niceness = Lower Priority. 19 is the least important, -20 is the most important.
  • Permissions: You can always increase the niceness (make a process "nicer"), but you cannot decrease it (make it "meaner") unless you are the root user or have sudo privileges.
  • Excessive Throttling: If you set a critical system service to 19, it may become sluggish or fail to respond to network requests under heavy load. Use extreme values only for non-essential background work.

FAQ

Can I use nice on a shell script that launches other commands? Yes. Any child process spawned by that script will inherit the niceness value of the parent shell.

What happens if I set everything to -20? The kernel will struggle to maintain order, and the system may become unresponsive, as no process is willing to yield CPU time.

Does nice affect RAM or Disk I/O? No. nice only controls CPU scheduling. For Disk I/O management, you would look at tools like ionice.

Recap

  • nice: Sets the priority of a process as it starts.
  • renice: Adjusts the priority of a process currently running.
  • Niceness Range: -20 (highest priority) to 19 (lowest priority).
  • Rule of Thumb: Use higher values (10–19) for background tasks that shouldn't impact performance.

Up next: We will explore how to bundle repeated logic into reusable Shell Functions to keep your configuration scripts clean and maintainable.

Similar Posts