Changing Priority of Process in Linux

What is Priority?

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.

1.Checking the Priority of Running Processes?



Another way you can get the nice value using ps command

#ps -o pid,comm,nice -p 2077   //2077 is the process ID


[root@serverA ~]# ps -o pid,comm,nice -p 2077
  PID COMMAND          NI
 2077 scopeux         -20
[root@serverA ~]#

2.Setting priority on new processes?

To change the priority when issuing a new command

#nice -n [nice value] [command]


#nice -n 10 apt-get upgrade

This will increment the default nice value by a positive 10 for the command, ‘apt-get upgrade‘

3.Setting Priority on Existing/running Processes?

To change the priority of an existing process just do

#renice [nice value] -p [process id]


#renice 10 -p 2077

This will increment the priority of the process with an id of 21827 to 10.

4.Setting Permanent Priority on all Processes for a Specific User ?

Sometimes it is helpful to give specific users lower priority than others to keep system resources allocated in the proper places like core services and other programs.

You can set the default nice value of a particular user or group in the /etc/security/limits.conf file.

#vi /etc/security/limits.conf

dbuser hard priority 1

>save and exit

It uses this syntax: [username] [hard|soft] priority [nice value]

Post a Comment