How To Change The Execution Priority Of A Process In Linux?


In Linux, a process is an instance of a program that is running on a computer. When a program starts executing it can have multiple processes associated with it. These processes can either run by a user or by the operating system itself.  Usually, a process terminates on its own when they are done with their task.

You can change the process priority by using nice and renice commands. The process scheduling priority is ranged from -20 to 19. We can call this nice value. A process with the highest priority will have -20 nice value while a process with the least priority will have 19 nice value.

By default when a process starts it gets 0 nice value.

Display the nice value of a process

You can use the following command in your terminal to display the current priority of a process.

top

This will display output as given below. The NI column in this output shows the niceness or nice value of processes.

 

How to change the priority of an existing process?

Now if you see in the image above mysqld has process id 893 and NI value 0. To change the priority or nice value of an existing process, we will use the following command –

renice  -5 -p 893

After executing this command nice value of mysqld will be changed from 0 to -5.

How to set the priority of a new process?

You can set the priority of a new process by adding the nice command with the command that you are going to run. The syntax of the command can be given as –

nice -n [nice value] [command]

For example –

nice -n 10 sudo apt upgrade

This will start upgrading with a priority of 10 which is less prior then the default value 0. This is useful when you want to upgrade the system but don’t want an extra process burden at a given time.

How the change the priority of processes belongs to a particular group?

To change the priority of all the processes belongs to a particular group use -g option with the renice command. Use the following command –

renice -n 5 -g hr

This will change the current nice value of all processes belongs to group hr to 5.

How to change the priority of all processes owned by a user?

Use option -u to alter the priority of processes owned by a user. You can use the following command to change the priority of processes owned by user lalit.

renice -n -5 -u lalit

The current nice value of all the processes will be changed to -5 which is more prior then the default nice value 0.

Conclusion

By reading this article you are now able to change the priority of a process. If you have any thoughts on this topic then share it with us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.