In a recent article, we discussed how to end a running process ( See: Killing Linux Processes ). Today we are discussing how to reprioritize or adjust process priority with nice and renice linux commands.
Nice Value – Low or High Prioriry
Every process running on your system has a “nice” value telling the kernel what priority should be given to that specific process. Positive values assign a lower priority to your process, while negative values assign a higher priority. The full spectrum range of nice values goes from -20 (highest priority) to 19 (lowest priority). One will want to be extremely cautious using the lowest numbers or highest priority as you can crash your system by prioritizing your processes higher than critical system processes.
Every process has a nice value, including my bash shell.
Example: Finding our shell’s nice value
$ nice 0
Example: Use the tar command with the lowest priority (19) to backup a directory.
$ nice -n 19 tar -cvf /home/mary/uptimemadeeasy.tar /path/mydirectory
While my process is running, I can use top to see the “nice” value of my tar command:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 8185 apache 20 0 448m 56m 4120 R 51.7 5.7 14:37.30 httpd 10304 jstaten 39 19 113m 1316 1040 R 7.3 0.1 0:03.46 tar 840 root 20 0 0 0 0 S 3.3 0.0 47:11.89 flush-253:0
Note, only a privileged user such as root can run a process with a negative (high priority) value.
Changing a Process Niceness Using Renice
Now and then, we may want to change the priority of commands that are already running to help them finish quicker, or to not interfere with other users’ processes.
Example: Change PID 11718’s nice value from its current value to -3
# renice -3 11718 11718: old priority 15, new priority -3
Interestingly, while a regular user can decrease a process’ priority they are unable to increase the priority of a process.
Example: A regular user attempts to increase the priority of one of their processes
$ renice -3 11918 renice: 11918: setpriority: Permission denied
A nice “Permission denied” message is returned when a regular user attempts to increase the priority of a process.
The root account, on the other hand, is able to increase or decrease any user’s process priority.
One can also use the renice command to change all of a user’s processes.
Example: Change the priority of all of mary’s processes
# renice +4 -u mary 500: old priority 0, new priority 4
Others have written articles or forum responses on the nice command: