The nice Command

nice is a scheduling command used to tell a running process what priority it has in the process queue. By default, in a UNIX system all processes run with the same priority - no one is more important than the other. They all have an equal share of the CPU's time. With nice you can inform the kernel that the process is more or less important, and thus cause that process to either have a larger slice of the CPU's time or a smaller amount.

Note: nice affects how the process is scheduled with respect to other running processes. A program that has been niced down to minimal priority will still occupy 100% of the CPU's time if no other process is currently running. Likewise, a process with maximum priority will still cause other processes to execute - they will simply execute more slowly.

Using nice

nice can be used whenever when a program is initially launched, or it can be used to modify an already existing process.

Launching Programs With nice

To launch a program with nice, simply execute the command nice, followed by the priority modifier, followed by the program command. The priority modifier ranges from 19 (lowest priority) to -20 (highest priority). In general, users will typically want to lower the priority of large jobs to increase system response for other users.

For example, to start a large compile job with priority 10 (low priority), the following command could be issued:

nice -n 10 make large_job

For a high priority job, the following command could be used:

nice -n -20 <command>

When given with no -n option, nice defaults to -n 10.

Important: nice hints to the operating system that it wants to modify its priority, the OS is under no obligation to obey nice restrictions. Lowering the priority will always work, raising the priority will only work if the OS doesn't have more important things to do.

Warning! nice is a program, but it's also a part of some shells. Which nice is used can depend on whether the command is issued interactively or as part of a script. When in doubt, you can always explicitely specify the nice program by calling it with its full path: /bin/nice

Changing the Priority of Running Processes

To change the priority of a process that is already running, you need to use the renice command. You can specify processes by UID, GID, username or PID. For example, to change the priority of all processes running under user wyvern issue the following command:

renice +5 -u wyvern
515: old priority 0, new priority 5

This changes the priority of all of user wyvern's processes to +5, which is lower priority than the default, which is 0.

To change the priority of a single process, you will need to know it's PID, or process identifier. The easiest way to do that is to issue the command ps -ef | grep <username> or <process>, and locate the process you're interested in. For example, say I opened a file in vi and then wanted to renice the process to the lowest possible priority. First I would locate the PID of the process like so:

ps -ef | grep wyvern or optionally ps -ef | grep vi

Then find the PID of the job (we'll use 500 as an example) and issue renice as follows:

renice +19 -p 500

See the manpage (man renice) for more information.

Important: On multi-user systems like the Fates, it's important (and polite) that you nice your jobs appropriately. Don't run programs at a higher priority than default, and always nice +10 or +19 large jobs that take a while to complete.

Back to Navigation