PostgreSQL User and Group priority,nice,Renice value Changing
A process is a running program. So, any program running on your Linux box is a process. One or more processes can be running on your Linux box at a time. We can display the information about active processes using ps command. By default, ps command only shows current user’s processes. You can use “ps -ef” (without quotes, of course) command to display all processes. To view the user of a process, we use “ps -u”. Hope you got a basic idea about Linux processes. Let us now come to the point. This tutorial addresses how to change the priority of a Process in Linux using nice and renicecommands..
As you might know, by default, Linux kernel considers all processes equally important and allocates the same amount of CPU time for each process. Sometimes, you might want to increase or decrease the priority of certain processes to utilize more CPU time. This is where the nice and renice commands comes in help. Nice command is used to run a process with an user defined priority whereas renice command is used to change the priority of a running process. Generally, nice and renice commands are used to change the priority than the default priority of a process.
Change The Priority Of A Process With Nice command
View the default priority of a Process
Whenever a process starts normally, it gets the default priority value of zero.
We use cmus program, a command line media player for the purpose of this guide.
Run it using command:
Note: If a command is terminated by the control operator &, the shell executes the command in the background in a subshell.
Now let us see the running processes using command:
Sample output:
As you see in the above output, the niceness value is listed under the column heading “NI”. So, it is clear that all processes gets the default priority value of zero.
Start a process with nice utility
If you start a process with nice command without any arguments, it gets the default value of 10. Here 10 is the niceness value or priority. Niceness values range from -20 (most favorable to the process) to 19 (least favorable to the process). To put this simply, the negative values (Eg. -20) gives higher priority to a process and positive values (Eg. 19) gives lower priority.
Now, let us stop the running program cmus with command:
Here, 13829 is the PID of cmus program. You will see this value under heading “PID” in the above output.
Now, run cmus program with ‘nice’ utility.
Check the running processes with command:
Sample output:
See, now cmus process got the default niceness value of 10, which means its priority has been changed.
Start a process with nice utility with lower priority
Also, you can start the same process with specific niceness value, for example 15, like below.
Kill the process with with kill command:
Again, start the process with niceness value of 15:
Note: Here, do not confuse – (hyphen) with minus. We use – (hyphen) to pass a value. Hence, to assign a positive value, we use -15. Likewise, to assign a negative value, we use –15 (i.e double hyphen).
Let us check the niceness value using ps -al command:
Sample output:
See, now cmus process got the niceness value of 15.
In the above examples, we launched the processes with lower priority. Remember positive niceness value represents lower priority.
Start a Process with Nice utility with higher Priority
You already know that negative niceness value represents higher priority. Please note that regular users are not allowed to start a process with higher priority. You need to be root user to launch any process with higher priority.
So, to start a process using nice utility with lower priority, for example -15, run the following command:
Sample output:
Start processes with -n option
You can also use -n option to increase or decrease the priority.
To increase priority, run the following command as root user:
To decrease priority, run the following command as normal user:
Change The Priority Of A Running Process With Renice command
What we have seen so far is we launched a process with an user defined priority. Now, we are going to change the priority of a running process using renice command.
Let us see the running processes using command:
Sample output:
As you see in the above output, cmus is running with lower priority value of 15.
To change the current niceness value of a running process, for example 18, run the following command:
Here, 15059 is the process id of cmus process.
Sample output would be:
Verify the niceness value using command:
Sample output:
To change it to negative niceness value (to assign higher priority), run the following command as root user:
You can even change the priorities of all running group belongs to a particular group like below.
The above command changes the priority of all running processes which are belongs to a group called ostechnix.
And also, you can change the priority of running process owned by particular user, for example sk, using command:
For more options, I suggest you to refer the man pages of nice and renice utilities.
Conclusion
I hope you got a basic idea about what is a Linux process, and how to change its priority using nice and renice utilities. If you find this guide useful, please share it on your social and professional networks and support OSTechNix. Go and nice your programs. I will be soon here with another interesting and useful guide.
Comments
Post a Comment