Monday, 24 April 2017

nice command usage and examples

nice
Linux Kernel schedules the process and allocates CPU time accordingly for each of the process. Every running process has a priority assigned to it. When a process is started, by default it gets launched with the priority of '0'. We can change the process priority either nice or renice commands. We can only use nice command to change the process priority when the process is started. But we cannot use nice command to change the priority of an already running process.
Syntax
nice [option] [command [arg]...]
Option
-n, --adjustment=N add integer N to the niceness

By using nice command we can start a new process with required priority.But the process can be set with priority range only from -20 to 19. A process with the nice value of -20 is considered to be on top of the priority. And a process with nice value of 19 is considered to be low on the priority list. The default niceness for processes is inherited from its parent process and is usually 0.If we give a process a higher priority, then Kernel will allocate more cpu time to that process. Normal users can only decrease the process priority. However root user can increase/decrease all process's priority. Running nice command with no options will give the priority with which the current shell is running.
The process priority can be seen by running one of the following commands:

ps –al

ps -o pid,comm,nice -p actual_pid

top

The column that starts with "NI" shows the nice value i.e. the priority of the process.

Setting less priority to a process
Any greater number will have highest less priority than the preceding numbers.

Example
nice -n 19 tar cvzf archive.tgz largefile

Setting high priority to a process
Any smaller number will have highest high priority than the following numbers.

Example
nice -n -20 tar cvzf archive.tgz largefile

nice -20 tar cvzf archive.tgz largefile

nice --20 and nice -n 20 will do the same thing. Both the above commands will make the process priority to the value 20.

Setting permanent priority user specific on all processes
We can set the default nice value of a particular user or group in the /etc/security/limits.conf file.

Syntax
[username] [hard|soft] priority [nice value]

No comments:

Post a Comment