Example Uses of the Commands "nice" and "renice"

An Introductory Tutorial

nice and renice commands

Linux systems have the capability to run many processes ("jobs") simultaneously. Even if the CPU has multiple processors or cores, the number of processes generally far exceeds the number of available cores. It is the job of the Linux kernel to distribute the available CPU cycles to the active processes.

By default all processes are considered equally urgent and are allotted the same amount of CPU time.

In order to enable the user to change the relative urgency of processes, Linux associates a priority parameter with each job that can be set or changed by the user. The Linux kernel then reserves CPU time for each process based on their relative priority values.

Linux process priorities around defined by the "nice" parameter which ranges from minus 20 to plus 19. It can take on only integer values. A value of minus 20 represents the highest priority level, whereas 19 represents the lowest priority level. The highest priority level being indicated by the most negative number is somewhat unintuitive. However, the parameter is called "nice", and running at lower priority is considered nicer, since it allows other processes to use a bigger share of CPU time.

When you use the command nice you start a new process (job) and assign it priority (nice) value at the same time. In order to change the priority of a process that is already running you use the command renice.

The following command line starts the process "large-job" setting the "nice" value to 12.

nice -12 large-job
Note that the dash in front of the 12 does not represent a minus sign. It has the usual function of marking a flag passed as argument to the nice command.

If you wanted to set the nice value to minus 12, you would add another dash:

nice --12 large-job
Remember that lower nice values correspond to higher priority. So -12 has a higher priority than 12. The default nice value is 0. Regular users can set lower priorities (positive nice values). In order use higher priorities (negative nice values) administrator privileges are required.

You can change the priority of a job that is already running using renice.

renice 17 -p 1134
This changes the nice value of the job with process id 1134 to 17. In this case there is no dash for command option when specifying the nice value. The following command changes the nice value of process 1134 to -3:

renice -3 -p 1134
You can print out a list of current processes using the ps command. When you add the "l" (as in "list") option, the nice value is listed under the column heading "NI". For example:

ps -al