UNIX / Linux : How to change the niceness (priority) of a process

What is process priority (niceness)

All process when spawned, they are assigned a priority based on a numeric value called as “nice value“. The priority of a process denotes how much processor time allocated to that process. There are 40 niceness values, with –20 being the highest and +19 the lowest. Most system-started processes use the default niceness of 0. 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. A child process inherits the niceness of its calling process in calculating its priority.

Viewing the niceness of the process

You can view the niceness of the process using the command ps, top etc. To view the default niceness value use the below command:

# nice
0

To check the niceness of the currently running process :

# ps -elf

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]

For example to run the yum update command with nice value of +10 which gives it less priority over other processes. This makes sure that yum update does not load the system more.

# nice -n 10 yum update

Setting Priority of currently running process

To change the priority of an existing process use the renice command :

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

For Example to change the priority of the currently running process (with pid 390) to 15.

# renice 15 -p 390
390: old priority 0, new priority 15
Note: Only root can apply negative nice values.

Setting default niceness for a particular process

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.

– For users, it uses this syntax:

# vi /etc/security/limits.conf
[username] [hard|soft] priority [nice value]

– For groups, it uses this syntax:

# vi /etc/security/limits.conf
[@groupname] [hard|soft] priority [nice value]

For example, you can have below entries for user and group respectively.

# vi /etc/security/limits.conf
user01 hard priority -10
@group01 hard priority -10

This would add priority to all the applications running under user ‘user01’ or group ‘group01’ priority set to ‘-10’

Related Post