What is Priority and Why Should I Care?
When talking about processes priority is all about managing processor time. The Processor or CPU is like a human juggling multiple tasks at the same time. Sometimes we can have enough room to take on multiple projects. Sometimes we can only focus on one thing at a time. Other times something important pops up and we want to devote all of our energy into solving that problem while putting less important tasks on the back burner.
In Linux we can set guidelines for the CPU to follow when it is looking at all the tasks it has to do. These guidelines are called niceness or nice value. The Linux niceness scale goes from -20 to 19. The lower the number the more priority that task gets. If the niceness value is high number like 19 the task will be set to the lowest priority and the CPU will process it whenever it gets a chance. The default nice value is zero.
By using this scale we can allocate our CPU resources more appropriately. Lower priority programs that are not important can be set to a higher nice value, while high priority programs like daemons and services can be set to receive more of the CPU’s focus. You can even give a specific user a lower nice value for all of his/her processes so you can limit their ability to slow down the computer’s core services.
Source
Set the priority for new processes with nice, eg
nice -n 10 firefox
for existing processes
renice 10 -p $(pgrep firefox)
To set the priority <0 you need sudo, eg:
renice -1 -p $(pgrep firefox)
renice: failed to set priority for 2769 (process ID): Permission denied
but not for a priority >=0
Example
% ps -o pid,comm,pri,nice -p $(pgrep firefox)
PID COMMAND PRI NI
2769 firefox 19 0
% renice 10 -p 2769 # note, we don't need sudo here
2769 (process ID) old priority 0, new priority 10
% ps -o pid,comm,pri,nice -p $(pgrep firefox)
PID COMMAND PRI NI
2769 firefox 9 10
% sudo renice -19 -p 2769
2769 (process ID) old priority 10, new priority -19
% ps -o pid,comm,pri,nice -p $(pgrep firefox)
PID COMMAND PRI NI
2769 firefox 38 -19
Other example
To renice all running processes for a specific user
renice 20 -u user_name