In this lesson, you will learn how to manipulate the Linux scheduler’s "niceness" values to ensure your high-throughput shell jobs receive the CPU resources they need when the system is under heavy load.
The Concept of "Niceness"
In Linux, the CPU scheduler constantly decides which process runs next. By default, most processes are created with a "nice" value of 0. This value acts as a priority modifier ranging from -20 (the highest priority) to 19 (the lowest priority).
Think of it as a social ranking: a "nice" process (high value, like 19) is very polite—it steps aside and lets others take the CPU. A "not-so-nice" process (low or negative value) is assertive and demands more time from the CPU. When your shell environment is under heavy load, you can dynamically adjust these values to prevent a background data-processing task from choking your interactive shell.
Controlling Priority
When you launch a process, you can set its initial priority using the nice command. For example, if you are running a heavy calculation in the background, you can start it with a lower priority so it doesn't slow down your active terminal session:
nice -n 10 ./my-heavy-script.sh &
If you already have a process running and notice your system is sluggish, you can change its priority on the fly using the renice command. You identify the process by its Process ID (PID):
# Set the process with PID 1234 to be more "polite" (lower priority)
renice -n 15 -p 1234
Why Use This?
When your system is overwhelmed, the scheduler looks at these values to distribute CPU slices. By increasing the niceness of non-critical background jobs, you ensure that the processes you are currently interacting with in your TTY maintain responsive performance. This is particularly useful in shared environments or when running batch jobs alongside your primary workflow, ensuring that your most important work never gets "starved" of compute time by background clutter.
Key Takeaway: You can balance system resources by adjusting the "niceness" of processes, where higher values represent lower priority, allowing you to shield your interactive shell from heavy background workloads.