LinuxCommandLibrary

nice

Run programs with modified scheduling priority

TLDR

Launch a program with altered priority

$ nice -[niceness_value] [command]
copy

Define the priority with an explicit option
$ nice [[-n|--adjustment]] [niceness_value] [command]
copy

SYNOPSIS

nice [OPTION]... COMMAND [ARG]...

PARAMETERS

-n, --adjustment=ADJUSTMENT
    Add ADJUSTMENT to the niceness. ADJUSTMENT is an integer typically between -20 and 19. If omitted, the default adjustment is 10.

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The nice command is a fundamental utility in Unix-like operating systems, including Linux, used to invoke a command or program with a modified scheduling priority. This priority, known as the "niceness" value, influences how the system's CPU scheduler allocates processor time to a process. The niceness scale typically ranges from -20 (highest priority, least "nice" to other processes) to 19 (lowest priority, most "nice").

By default, most processes start with a niceness of 0. When you run a command using nice, you can specify an adjustment to this default value. A higher niceness value means the process is more "polite" and will yield CPU time to other processes, making it suitable for non-critical, long-running background tasks or resource-intensive computations that shouldn't hog system resources. Conversely, a lower niceness value (closer to -20) gives the process more CPU preference.

A key distinction is that nice is used to set the priority of a command before it starts executing. For adjusting the priority of an already running process, the renice command is used. Ordinary users are typically restricted to increasing the niceness value (making the process less demanding), while the superuser (root) can set any niceness value, including negative ones, to elevate a process's priority.

CAVEATS

  • Permissions: Non-root users can only increase the niceness value (make a process less prioritized) or set it to a value greater than or equal to the current niceness. Only the superuser (root) can set negative niceness values or decrease the niceness of a process to give it higher priority.
  • CPU Scheduling Only: The nice command primarily affects CPU scheduling priority. It does not directly control I/O priority, memory usage, or network bandwidth. For I/O prioritization, the ionice command is typically used.
  • Not a Guarantee: While nice provides a hint to the scheduler, the actual performance impact depends on overall system load, other running processes, and the specific scheduling algorithm implemented by the kernel (e.g., the Completely Fair Scheduler (CFS) in Linux). A very high niceness value on an otherwise idle system might not significantly reduce CPU usage.

NICENESS VALUE RANGE

The niceness value typically ranges from -20 (highest priority, least nice) to 19 (lowest priority, most nice). A lower numerical value means higher priority.

DEFAULT NICENESS

Unless specified with the -n option, a command invoked with nice will start with a default niceness adjustment of +10, meaning its actual niceness will be 10. If nice is run without any arguments (i.e., just `nice`), it prints the current niceness.

RELATIONSHIP WITH <I>RENICE</I>

While nice sets the priority of a process as it starts, renice is used to modify the niceness of processes that are already running. This distinction is crucial for managing dynamic workloads.

HISTORY

The concept of "niceness" and tools like nice have been integral to Unix-like operating systems since their early development. They emerged from the need to manage shared computing resources effectively, particularly CPU time, among multiple users and processes. The idea is to allow users to cooperatively yield CPU resources, ensuring that no single process monopolizes the system, especially for long-running, non-interactive tasks. This cooperative scheduling mechanism has evolved with different kernel schedulers (e.g., O(1) scheduler, CFS in Linux), but the user-facing interface via nice has remained consistent in its purpose: providing a simple way to adjust process priorities for better system responsiveness and resource allocation.

SEE ALSO

renice(1), nohup(1), ionice(1), ps(1), top(1), htop(1)

Copied to clipboard