Home / Alt manpages / nproc(1)

  • nproc(1)
  • User command
  • linux

Choose a Safe Parallelism Limit with nproc

nproc tells a script how many workers the current machine can actually handle. Hardcode -j 8 into a build script instead, and it breaks the moment someone runs it on a four-core laptop. The examples use GNU coreutils 9.4, installed here as Ubuntu package version 9.4-3ubuntu6.3. On this machine, nproc reports 8 processing units.

Allow about ten minutes. You need a shell and the coreutils package, normally already present on a Linux system. Every command below is read-only and runs without sudo. This guide does not change CPU affinity, services or persistent configuration.

1. Check the number available to this process

Run nproc with no options:

$ nproc
8

The output is one decimal integer followed by a newline: the number of processing units available to the current process. That can be lower than the number of processors online, which is exactly why it is usually the right value for deciding how much work a command should start in the current environment.

Checkpoint

Capture the value in a shell variable and inspect it before using it:

workers=$(nproc)
printf 'available workers: %s\n' "$workers"

Expected output on this host is available workers: 8. Keep the command substitution separate from later arithmetic, so a diagnostic message or unexpected output cannot silently sneak into a larger command.

2. Compare available and installed processors

Use --all when you need the number of installed processors rather than what is available to the current process:

$ nproc --all
8
  • Plain nproc describes this process's usable capacity.
  • nproc --all reports installed processors instead.
  • They can match by coincidence, as they do here at 8 and 8; do not swap one for the other just because a test happened to agree.

A useful diagnostic comparison:

printf 'available: '
nproc
printf 'installed:  '
nproc --all

Checkpoint

If the numbers differ, use the plain value for work launched by this process, unless the program has a separate, deliberate reason to target the machine-wide installed count.

3. Reserve capacity with --ignore

Pass --ignore=N to ask nproc to exclude up to N processing units:

$ nproc --ignore=1
7

Handy for a build or batch job that should leave one unit free for interactive work. The option says "if possible", so it is not a promise the result equals the plain count minus exactly N. nproc always keeps a usable result: asking to ignore more units than exist still returns 1 on this host:

$ nproc --ignore=999
1

--ignore=0 is equivalent to making no reservation at all:

$ nproc --ignore=0
8

Use a literal, reviewed number in a script. If the reservation comes from an environment variable, validate it before it goes anywhere near a command, especially in a script another user or service might call.

4. Turn the result into a bounded worker count

For a command that accepts a job count, calculate it once and pass it as one shell word. This example reserves one unit and prints the value a later command would receive:

workers=$(nproc --ignore=1)
printf 'job limit: %s\n' "$workers"
# Example shape for a tool with a -j option:
# build-tool -j "$workers" TARGET

Expected output here is job limit: 7. Only swap in build-tool and TARGET after confirming the real program actually uses -j for parallelism: nproc does not launch workers and does not configure whatever consumes its output.

Do not write an unquoted expansion such as -j $workers once the value comes from anything more complicated. Quoting makes the argument boundary explicit, and for a plain numeric output it also makes the script easier to audit.

5. Verify a script without changing system state

Make a small check fail clearly if nproc cannot provide a usable number:

workers=$(nproc --ignore=1) || {
    printf '%s\n' 'nproc failed; stopping' >&2
    exit 1
}

case $workers in
    ''|*[!0-9]*)
        printf 'unexpected worker count: %s\n' "$workers" >&2
        exit 1
        ;;
esac

printf 'using %s workers\n' "$workers"

With the installed command, normal output is using 7 workers. The check needs no elevated privileges, and there is nothing to undo: it only reads a value and sets a shell variable.

Common traps

  • Do not confuse installed processors with capacity available to this process. Plain nproc is the safer default for a command running in the current execution context.
  • --all is not a request for more capacity. It only selects a different number to print, not a way to make all installed processors available.
  • Do not read the output as idle time. nproc reports a count, not load average, frequency, temperature or memory capacity. A job can still run slowly with an accurate count, and a high count is no reason to drop other resource limits.
  • Check --help on a new host. The command also accepts --help and --version. This guide's observed behaviour is GNU coreutils 9.4, not a guarantee that another implementation named nproc accepts the same syntax.

Done means

  • Understand the difference between nproc and nproc --all.
  • Can reserve capacity with nproc --ignore=1 and know the lower bound of 1.
  • Script captures one value, quotes it, checks failure, and only passes it to a tool whose parallelism option you have verified.
  • No side effects: no sudo, no affinity changes, no altered persistent system state.