← All commands
THE COMMAND REFERENCE

nproc

Print the number of processing units available.

Last updated

BusyBox

Implementation-level reference; examples are not certified for every installed version.

Overview

nproc prints how many processors are available to the current process. Scripts use it to decide how many jobs to run in parallel, as in make -j"$(nproc)". The answer can be lower than the number of processors in the machine, for example inside a container with a limit.

Background

nproc is part of GNU coreutils and is also an applet in BusyBox. macOS does not ship it; there, sysctl -n hw.ncpu gives the processor count. They agree on printing the number.

Examples

Print the number of processors; the exact number depends on the machine, so the example only checks that a number is printed:

nproc | grep -c '^[0-9][0-9]*$'

Use it to choose a number of jobs, for example for make -j:

jobs=$(nproc); test "$jobs" -ge 1 && echo ok

Common mistakes

nproc honors limits such as the OMP_NUM_THREADS variable in GNU, so it may print less than the machine has; nproc --all ignores them. Using every processor for a build can make the machine unresponsive, so leave one or two free. On macOS, nproc is not available and a script needs sysctl -n hw.ncpu there.

Portability

This page has no Apple view: macOS has no nproc. A portable script can try nproc, then getconf _NPROCESSORS_ONLN, then sysctl -n hw.ncpu. The Common examples were executed on GNU coreutils 9.11 and BusyBox 1.37.0 (see the development guide for the exact check).

About BusyBox nproc

BusyBox bundles nproc as an applet, and anything beyond the common options depends on how it was built. This page's examples ran on BusyBox 1.37.0.

BusyBox notes

BusyBox nproc supports the options used in the Common examples. Others, and GNU long options, depend on the build.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.