Hack 100. Nice Command Examples
by Ramesh
Kernel decides how much processor time is required for a process based on the nice value. Possible nice value range is: -20 to 20. A process that has a nice value of -20 is very high priority. The process that has a nice value of 20 is very low priority.Use ps axl to display the nice value of all running process as shown below.
- # ps axl
- F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
- 4 0 1 0 16 0 2172 552 - S ? 0:17 init [5]
- 1 0 3 1 34 19 0 0 ksofti SN ? 3:18 [ksoftirqd/0]
- 1 0 10 1 5 -10 0 0 worker S< ? 0:01 [events/0]
- 4 0 5145 1 25 10 32124 18592 - SNs ? 0:08 /usr/bin/python /usr/bin/rhn-applet-gui --sm-client-id default4
- 4 0 5147 5142 16 0 3528 604 - S ? 0:00 /sbin/pam_timestamp_check -d root
- 1 503 17552 4180 16 0 14208 3920 - S ? 0:01 /home/www/apache2/bin/httpd -f /home/www/apache2/conf/httpd.conf -k start
How to assign a low priority to a shell-script? (higher nice value)
In the example below, when I started the nice-test.sh script in the background, it took the nice value of 0.
- $ ./nice-test.sh &
- [3] 13009
- $ ps axl | grep nice-test
- 0 509 13009 12863 17 0 4652 972 wait S pts/1 0:00 /bin/bash ./nice-test.sh
[Note: 6th column with value 0 is the nice.]
Now, let us execute the same shell script with a different nice value as shown below.
- $ nice -10 ./nice-test.sh &
- [1] 13016
- $ ps axl | grep nice-test
- 0 509 13016 12863 30 10 4236 968 wait SN pts/1 0:00 /bin/bash ./nice-test.sh
[Note: 6th column with value 10 is the nice value for the shell-script.]
How to assign a high priority to a shell-script? (Lower nice value)
In the following example, let us assign a nice value of -10 (minus 10) to the nice-test.sh shellscript.
- $ nice --10 ./nice-test.sh &
- [1] 13021
- $ nice: cannot set priority: Permission denied
Note: Only root user can set a negative nice value. Login as root and try the same. Please note that there is a double dash before the 10 in the nice command below.
- # nice --10 ./nice-test.sh &
- [1] 13060
- # ps axl | grep nice-test
- 4 0 13060 13024 10 -10 5388 964 wait S< pts/1 0:00 /bin/bash ./nice-test.sh
[Note: 6th column with value -10 is the nice value of the shell-script.]