Using 'Nice' and 'Renice' Commands in Linux

Manually allocate more or fewer CPU cycles to individual running jobs

Rear view of computer programmers using laptop at office desk

 

Maskot / Getty Images

Linux systems can run many processes (jobs) simultaneously. Even if the CPU has multiple processors or cores, the number of processes usually 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.

Use the nice and renice commands to force the kernel to reallocate CPU cycles to specific applications.

Use Nice to Get Priorities Straight

nice command

By default, all the processes are considered equally urgent and are allotted the same amount of CPU time. 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 its relative priority value.

The nice parameter is used for this purpose. It ranges from minus 20 to plus 19 and can take only integer values. A value of minus 20 represents the highest priority level, whereas 19 represents the lowest. The fact that the highest priority level is indicated by the most negative number is somewhat counter intuitive. However, running at a lower priority is considered "nicer," because it allows other processes to use a bigger share of CPU time.

How to Play Nice

Using the command:

nice
starts a new process (job) and assigns it a priority (nice) value at the same time. To change the priority of a process that is already running, use the command:
renice
For example, the following command line starts the process large-job, setting the nice value to 12:
nice -12 large-job 

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

To set the nice value to minus 12, add another hyphen:

nice --12 large-job

Remember that lower nice values correspond to a 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).To use higher priorities (negative nice values), administrator privileges are required.

Change the Nice Value

Change the priority of a job that is already running using renice. For example:

renice 17 -p 1134

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

renice -3 -p 1134