You're asking exactly the right question to connect the concept to action!
The commands used to manage process niceness are:
nice: Used to start a new command with a specified niceness value.renice: Used to change the niceness of an already running process.
Let's look at how to use them with examples:
1. nice (for new commands)
You use nice when launching a command you want to run with a specific niceness from the start.
Syntax: nice -n <niceness_value> <command>
- If you don't specify
-n,nicedefaults to a niceness of+10.
Examples:
- Start a script with low priority (niceness +15):
nice -n 15 ./my_long_running_script.sh - Start a data compression with default +10 niceness:
nice tar -czf backup.tar.gz /path/to/data - Start a command with high priority (niceness -5, requires
sudo):
(Remember, negative niceness often requires elevated privileges.)sudo nice -n -5 some_critical_command
2. renice (for running processes)
You use renice to change the niceness of a process that is already executing.
Syntax: renice <niceness_value> -p <PID>
- You'll need the Process ID (PID) of the process you want to modify. You can find this using commands like
ps aux,top, orhtop.
Examples:
- Change the niceness of a running process with PID 1234 to +19 (lowest priority):
renice 19 -p 1234 - Change the niceness of a running process with PID 5678 to -10 (high priority, requires
sudo):sudo renice -10 -p 5678
Quick Tip:
If you want to quickly test this, open two terminal windows.
In Terminal 1, run:
yes > /dev/null
Then in Terminal 2, find the PID of the yes command using htop or pidof yes. Let's say the PID is 12345.
Then renice it:
renice 19 -p 12345
You might notice a difference in system responsiveness or how htop displays the CPU usage for yes when it's being "nice" vs. its default state.
These commands give you power over how your processes share the CPU! Let me know if you want to try an example!