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.]

{ 7 comments… read them below or add one }

1 Arun June 3, 2010 at 10:28 pm

In redhat linux RHEL4 etc.
renice 5 -p

This is the command, where 5 is the nice value. The -n is omitted. The right man page is man 8 renice

2 Loka January 21, 2012 at 3:25 am

@Arun renice is another command, and to do something else than what Ramesh explain ! So of course, different commands can have different syntaxes … And not only on RHEL? releases.

nice –> To start a process with a different priority than the default one
renice –> To change the prority of a already running process

3 sudhakar May 5, 2012 at 12:27 am

Hi
When i used this command on 2.6.18 kernel ,its not changing the nice value of my script program.
Regards,
sudhakar

4 guest April 10, 2013 at 12:17 pm

Awesome, Great info.
Thanks

5 Senthil July 17, 2013 at 5:54 am

It could be a typo error, Possible nice value range is: -20 to 19, not -20 to 20.

Please update.

6 swathi February 3, 2014 at 2:21 am

What if nice value is >20 .. say like 30

7 Pratik March 14, 2014 at 3:30 am

Thanks a lot.

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: