nproc
Print the number of available processors
TLDR
Display the number of available processing units
Display the number of installed processing units, including any inactive ones
If possible, subtract a given number of units from the returned value
SYNOPSIS
nproc [OPTION]...
PARAMETERS
-all, --all
Print the number of installed (configured) processors. This might include CPUs that are offline or reserved, differing from the default 'online' count.
-ignore=N, --ignore=N
Subtract N from the number of processors reported. Useful for reserving cores for other tasks or for systems with known performance bottlenecks.
--help
Display a help message and exit.
--version
Output version information and exit.
DESCRIPTION
The nproc command in Linux is a utility designed to print the number of processing units available to the current process or system. It serves as a straightforward way to query system hardware capabilities, particularly useful in scripting and automation where the number of available CPU cores dictates optimal parallelism.
By default, nproc reports the number of "online" processors, which are those currently active and available for use by the operating system. This is typically what applications and build systems need for dynamic resource allocation. For instance, a common use case is with the make command, where `make -j$(nproc)` automatically sets the number of parallel jobs to the available processor count, significantly speeding up compilation times.
The command derives its information from kernel interfaces, such as `sysconf(_SC_NPROCESSORS_ONLN)` or `sysconf(_SC_NPROCESSORS_CONF)`, rather than parsing files like `/proc/cpuinfo` directly, which makes it more robust and portable across different Linux distributions and kernel versions. This abstraction simplifies resource discovery for developers and system administrators, allowing them to write more adaptable scripts and programs that can optimize their performance based on the underlying hardware.
While nproc primarily focuses on logical processors (including hyper-threading cores), it offers an option to report the total number of installed processors, which might include those currently offline or reserved. It's an indispensable tool for efficient resource management in parallel computing environments.
CAVEATS
nproc typically reports the number of logical processors, which includes hyper-threading cores. For instance, a 4-core CPU with hyper-threading enabled will often report 8 processors.
In containerized environments (e.g., Docker, Kubernetes) or virtual machines, the reported number of processors reflects the CPU resources allocated to the container/VM, which may be less than the host's physical cores.
Using the `--ignore=N` option with a large value of N can result in a non-positive (zero or negative) number of processors being reported, which might cause issues for scripts expecting a positive count.
DEFAULT BEHAVIOR
By default, nproc outputs the number of online (available) processors, which are those currently active and available to the operating system and typically what applications use for parallel execution.
LOGICAL VS. PHYSICAL PROCESSORS
The output of nproc typically represents logical processors (CPU threads). For example, a CPU with 4 physical cores and 2 threads per core (like Intel Hyper-Threading) would usually report 8 logical processors. Use `lscpu` for more detailed physical CPU topology.
COMMON USE IN SCRIPTING
nproc is most famously used in `Makefile`s (e.g., `make -j$(nproc)`), where it dynamically configures the number of parallel compilation jobs to match the system's available CPU resources, significantly improving build times and system utilization.
HISTORY
The nproc command is part of the GNU Core Utilities (coreutils) package, a fundamental collection of tools for GNU/Linux operating systems. It was introduced to provide a standardized and portable way to determine the number of available processors. Prior to its widespread adoption, developers often had to resort to parsing `/proc/cpuinfo` directly or using less specific commands like `getconf NPROCESSORS_ONLN`, which could be more prone to parsing errors or less intuitive for general use. nproc offers a simpler, dedicated interface, streamlining resource discovery in shell scripts and build systems, reflecting the ongoing evolution of Linux utilities towards greater user-friendliness and robustness.