Set priority of process using nice command in linux

 Set Priority of Process with nice command in Linux

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 in to 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 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 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.

There are two options to reduce/increase value of a process. You can either do it using the nice command in Linux or the renice command.

Examples:

Use nice command in Linux  to schedule a priority of a process before starting it

Set a priority to a process before starting it, the syntax is

#nice  -n <nice value range (-20 to 19)> <command>

#nice  -n 5 cat > ktfile

 

Log in to other terminal and check the nice value for the above command/process.

#ps  -elf

 

How to change the nice value of any process while it is running.

To reschedule the nice value of existing process, first check the PID of that process by running #ps –elf command.

As from previous task we know the PID of cat command i.e. 13155

The following command used for renice the value of a cat command which is still running

  #renice <nice value (-20 to 19)> <PID>

#renice 2 13155

 

Leave a Reply

Your email address will not be published. Required fields are marked *