Home / Alt manpages / nproc(1)

  • nproc(1)
  • User command
  • linux

Choose a Safe Parallelism Limit with nproc

You will finish with a small, repeatable way to choose a worker count for shell scripts and build commands. 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, which is 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. It is the number of processing units available to the current process. That can be lower than the number of processors online, so this is usually the useful value for choosing 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 or unexpected output cannot silently become part of a larger command.

2. Compare available and installed processors

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

$ nproc --all
8

On this host both values happen to be 8. They answer different questions. Plain nproc describes the current process's usable capacity; nproc --all reports installed processors. Do not replace one with the other merely because their output matches during a test.

A useful diagnostic comparison is:

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

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

$ nproc --ignore=999
1

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

$ nproc --ignore=0
8

Use a literal, reviewed number in a script. If the reservation comes from an environment variable, validate it before placing it in a command, especially when the script may be called by another user or service.

4. Turn the result into a bounded worker count

For a command that accepts a job count, calculate the count once and pass it as one shell word. This example reserves one unit and prints the value that 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. Replace build-tool and TARGET only after checking that the real program uses -j for parallelism. nproc does not launch workers and does not configure the command that consumes its output.

Do not write an unquoted expansion such as -j $workers when the value is produced by a more complicated command. Quoting makes the argument boundary explicit. For this simple numeric output, the quoted form is also 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, the normal output is using 7 workers. The check does not need elevated privileges, and there is nothing to undo because it only reads a value and sets a shell variable.

6. Avoid the common interpretation errors

Do not confuse installed processors with capacity available to this process. Plain nproc is the safer default for a command that runs in the current execution context. Conversely, --all is not a request to make all installed processors available; it only selects a different number to print.

Do not treat the output as a measurement of current CPU idle time. nproc reports a count, not load average, frequency, temperature or memory capacity. A job can still run slowly when the count is accurate, and a high count is not a reason to remove other resource limits.

The command accepts --help and --version as well as the options used above. Check the local help when moving a script to another coreutils version. This guide's observed behaviour is GNU coreutils 9.4, not a guarantee that unrelated implementations named nproc accept the same syntax.

Done means

  • You can explain the difference between nproc and nproc --all.
  • You can reserve capacity with nproc --ignore=1 and understand the lower bound of 1.
  • Your script captures one value, quotes it, checks failure, and passes it only to a tool whose parallelism option you have verified.
  • You have not used sudo, changed affinity or altered persistent system state.