Linux process priority and how to change it with nice command

Each command or program or bash script than runs on Linux is called process. It contains the program code and its current activity. On Linux each process has priority. I am not going into details about how this is managed. But I am going to write about real live scenarios where you can use priority parameter of the process. There is a command Linux (Nix) command called nice which manages process priority. We will look into some examples of usage of this command. On Linux or any other Nix operating system process priority is a number between -20 and 19. A niceness of −20 is the highest priority and 19 is the lowest priority. And this number tells operating system (OS) how much CPU time to give to this process compared to other processes.

When you start a program it usually has priority of 0. If you want to start a program with another priority you can use command nice which is installed by default on Ubuntu, Debian, Red Hat, Cent OS and any other Linux distros.

nice -n N command paramaters

When you start a nice without parameter like this default value of priority is 10.

nice command paramaters

Note that on most systems you do not need permissions to set lower priority (values above 0). But if you want to set higher priority (values below 0) then you need to be root or to modify file /etc/security/limits.conf and to add correct permission to user which will use this command.

But let’s start with examples. Let’s create one simple bash script that calculates pi up to 50 000 digits. Here is the script.

howopensource@debian:~/nice$ more

PI=$(echo "scale=50000; a(1)*4" | bc -l)

echo $PI > pi.txt

Ubuntu Process Priority Command

What the script does is simply to calculate PI, store it into a variable and then print it to some output file. Idea is to simulate a lot of CPU usage. For example this script but with a scale of 5000 (not 50 000) runs for about 30 seconds on my computer. I didn’t wait to calculate pi with scale of 50 000. But idea is to keep CPU busy and to experiment with processes.

First example is to run the script in background and then to type top to see what happened.

howopensource@debian:~/nice$ ./ &
[1] 9789
howopensource@debian:~/nice$ top

Process Priority Ubuntu Linux

Then we do not need to wait to program to finish. Simply type fg to bring process into foreground and then interrupt it with Ctrl+C. Command fg comes from foreground and idea is to bring process into foreground. Remember when you start command with & then it runs in background, so while this program runs you can run another program or script. At the end you can run top to verify that program stopped running.

howopensource@debian:~/nice$ fg
howopensource@debian:~/nice$ top

Next step is to run the same command with lower priority. Remember priority range is from -20 to 19, when you run program in a normal way it starts with priority 0 and when you run script with nice without parameters then default priority is 10. So let’s see what happened.

howopensource@debian:~/nice$ nice ./ &
[1] 9844
howopensource@debian:~/nice$ top

Linux Top Process Priority Example

Next experiment is to run the same command two times – once without nice with default priority of 0 and second time with nice and default priority of 10. We should see than process with priority 0 occupies more CPU time. Here is the scenario and the output of it.

howopensource@debian:~/nice$ ./ &
[1] 9868
howopensource@debian:~/nice$ nice -n 10 ./ &
[2] 9873
howopensource@debian:~/nice$ top

Low and High Priority commands Ubuntu

Where to use nice command

Here are some practical ideas where you can use it. For example I backup this web site each night with a cron job. And script that runs is very simple. It simply calls tar and mysqldump commands. Here is the simple backup script that handles backup of


cd /home/howopensource/backup

DF=`date +%Y%m%d`

nice -n 15 tar -cf wp-$DF.tar /var/www/HowOpenSource
nice -n 15 gzip wp-$DF.tar

nice -n 15 mysqldump -u root -p****** wpdb_howopensource > wpdb-$DF.sql
nice -n 15 gzip wpdb-$DF.sql

The idea is to backup site data during the night but to start backup processes with lowest priority in order to give more CPU time to Apache and MySQL to process user requests. You can use nice in similar places like cron jobs that do maintenance job and do not need higher priority.

Related Posts Plugin for WordPress, Blogger...