Change the priorities of linux processes with nice and renice

Every process running on the linux system has a default priority assigned which tells the the system how much processing power should be dedicated to each particular process. It is possible to change this priority value with nice or renice command. Here is a small example: Let’s say that we have a very simple bash script which prints date and time to the file 1000 times.

#!/bin/bash
for i in $(seq 1 1000); do date >> date.txt;sleep 1; done

Save this script and make it executable with chmod command. Execute the script:

$ nice -n 00 ./date &

By executing a ./date script with nice -19 command we gave the date.sh very high priority as the priority range is from -20 ( higest ) to 19 ( lowest ). to confirm the priority run ps -l command from the same terminal.

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  3670  2940  0  80   0 -  1196 -      pts/2    00:00:00 bash
0 S  1000  6665  3670  0  90  10 -  1111 -      pts/2    00:00:00 date.sh
0 S  1000  6697  6665  0  90  10 -   754 -      pts/2    00:00:00 sleep

as you can see date command and sleep command has same priority 10. However sleep running inside a date.sh is a child process of date.sh and date.sh is a parent process of sleep. At this stage we can try to change the priority to some other value with renice command and with use a PID ( Process Identification Number ):

$ renice 15 -p 6655
6665: old priority 10, new priority 15

The command above will change nice value from 10 to 15. to confirm priority value:

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  3670  2940  0  80   0 -  1198 -      pts/2    00:00:00 bash
0 S  1000  6665  3670  0  95  15 -  1113 -      pts/2    00:00:00 date.sh
0 S  1000  7109  6665  0  95  15 -   754 -      pts/2    00:00:00 sleep

NOTE: With a non-super user account you can change priorities only for the processes you own, assign priorities only in range of 0 – 19 and you can only increase a nice value. root user can change any process to any priority nice value. Another way to change priority values is to use top command and r key.