The Problem with CPU Hogs
Unlike a single-user laptop, a Linux server is designed to run hundreds of different background processes (daemons) and service dozens of different users simultaneously. To ensure the server doesn’t freeze, the Linux kernel uses a scheduler to determine exactly how much CPU time each individual process is allowed to consume.
By default, almost every process is given an equal priority. However, there are times when this egalitarian approach causes problems. For example, if you start a massive 50GB video rendering job or a massive database backup, that process will immediately consume 100% of the CPU, causing the company’s main web server to slow to a crawl.
To fix this, you do not need to kill the backup job. You simply need to teach it to be “nicer” to the other processes. You can do this dynamically, while the program is actively running, using the renice command.
Understanding Niceness Values
In Linux, process priority is dictated by a “niceness” value. This number dictates how nicely the program plays with others.
The scale runs from -20 to 19:
- 0: The default baseline priority given to almost every standard program.
- 19 (Maximum Niceness): The absolute lowest priority. The process will step aside and only use the CPU if absolutely nothing else on the entire server needs it. This is perfect for massive, non-urgent background tasks (like backups).
- -20 (Minimum Niceness): The absolute highest priority. This process is selfish, ruthless, and will steal CPU cycles from everything else to ensure it runs as fast as mathematically possible. (You must have
sudoroot privileges to assign negative numbers, as giving the wrong program a -20 will instantly crash the server).
Step 1: Finding the Process ID (PID)
Before you can change a program’s priority, you must know its unique Process ID (PID).
You can find this using the top command or by using the pidof command.
If the program hogging your CPU is a script named massive_backup.sh, run:
pidof massive_backup.sh
The terminal will output a number, for example: 4092. This is your target.
Step 2: Downgrading Priority Using renice
Now that you know the backup script is PID 4092, you want to demote it to the lowest possible priority so it stops interfering with the web server.
You use the renice command, followed by the new nice value you want to assign (e.g., 19), followed by the -p (PID) flag and the target number.
renice 19 -p 4092
The terminal will instantly reply: 4092 (process ID) old priority 0, new priority 19.
The backup job will continue to run, but the Linux kernel will now throttle it aggressively, ensuring the web server receives all the CPU power it needs to serve customer traffic smoothly.
Step 3: Upgrading Priority Using renice
Conversely, if you are running an incredibly urgent video rendering job and you want it to finish as fast as possible, you can give it a negative (selfish) nice value.
Because assigning negative numbers can destabilize the system, you must run the command with sudo.
Assuming the video renderer is PID 8832, run:
sudo renice -10 -p 8832
The kernel will instantly prioritize this task above all standard background processes, dedicating the vast majority of the CPU hardware specifically to finishing that render.
Alternative: Using the nice Command
The renice command is used to change the priority of a program that is already running.
If you are about to launch a script and you already know it is going to be a CPU hog, you don’t have to launch it and then scramble to find its PID to renice it. You can simply launch it with a modified priority from the very beginning using the standard nice command.
nice -n 15 ./massive_backup.sh
This command safely launches the backup script with an initial, harmless priority of 15.