Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How can you prioritize the processes in a Linux server ?

hint : nice command

user-image
Question added by Roshan Firozkhan , IT System Administrator , Emirates Driving Institute
Date Posted: 2015/03/30
Muhammad Anzar
by Muhammad Anzar , DevOps/DevSecOps Architect , Confidential

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 to19. The lower the number the more priority that task gets. If the niceness value is high number like19 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 programs like daemons and services can be set to receive more of the CPU’s focus. You can 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.

Checking the Priority of Running Processes

The easiest way to get a quick picture of what the current niceness priority on a process is to open up the top processes with:

top

Top displaying nice value

Another way you can get the nice value is use ps:

ps -o pid,comm,nice -p594

This will output the process ID, command, and nice value (marked as NI).

ps showing process priority

Setting priority on new processes

At this point you are probably wondering how you can set your own priority levels on processes. To change the priority when issuing a new command you do nice -n [nice value] [command]:

nice -n 10 apt-get upgrade

This will increment the default nice value by a positive10 for the command, ‘apt-get upgrade’ This is often useful for times when you want to upgrade apps but don’t want the extra process burden at the given time. Remember a positive number is gives less priority for a process.

Setting Priority on Existing Processes

Obviously at some point you are going to want to alter the nice value of something that is already running. Just the other day I was doing an upgrade to Ubuntu Jaunty and Firefox started to become unusably slow. A quick renice command rescheduled the upgrade to a lower priority and I was back to surfing in no time.

To change the priority of an existing process just do renice [nice value] -p [process id]:

renice10 -p21827

This will increment the priority of the process with an id of21827 to10.

Note: Only root can apply negative nice values.

renice

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.

/etc/security/limits.conf

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

backupuser hard priority1

Arunprakash kanagasabapathi
by Arunprakash kanagasabapathi , Senior System Engineer , ITQAN

 Linux process priorities around defined by the "nice" parameter which ranges from minus20 to plus19. It can take on only integer values. A value of minus20 represents the highest priority level, whereas19 represents the lowest priority level. The highest priority level being indicated by the most negative number is somewhat unintuitive. However, the parameter is called "nice", and running at lower priority is considered nicer, since it allows other processes to use a bigger share of CPU time.

When you use the nice command  you start a new process (job) and assign it priority (nice) value at the same time. In order to change the priority of a process that is already running you use the command renice.

Example:

nice --12 large-job

renice17 -p1134

 

 

More Questions Like This