Skip to Content
gnux

nprocx

Print the number of processing units available.

Mirrors GNU nproc. Run it as nprocx, or as gnux nprocx.

Synopsis

nprocx [OPTION]...

GNU reference

Description

nprocx prints the number of processing units available to the current process. As in GNU nproc, the OMP_NUM_THREADS environment variable (its first value, when it is a comma-separated list) replaces that count when it is a positive number, and OMP_THREAD_LIMIT caps the result. --all prints the number of installed processors instead and ignores both variables. --ignore=N subtracts N, but the result is never less than 1. gnux has no cgo, so both counts come from Go's runtime.NumCPU: the processors this process may run on. A machine with processors taken offline, or a process restricted to fewer of them, prints the same number with and without --all.

Permissions

Needs no permissions. Reads only the process's CPU affinity and its environment.

Use cases

  • Choosing how many parallel jobs a build should run.
  • Leaving a core free for the rest of the system with --ignore=1.
  • Respecting OMP_NUM_THREADS when a job scheduler has set it.
  • Reporting the machine's size in a CI log or a bug report.
  • Sizing a worker pool in a portable script.

Flags

      --all        print the number of installed processors
      --help       display this help and exit
      --ignore N   if possible, exclude N processing units (default "0")
      --version    output version information and exit

Examples

nprocx
nprocx --all

Leave one core free

nprocx --ignore=1

Run a build with one job per core

make -j"$(nprocx)"

Respect an OpenMP thread count

OMP_NUM_THREADS=4 nprocx

Cap the count with OMP_THREAD_LIMIT

OMP_THREAD_LIMIT=2 nprocx

Set the count through envx, on any platform

envx OMP_NUM_THREADS=3 nprocx

Save the count in a variable

jobs=$(nprocx --ignore=2); echox "using $jobs jobs"
printfx 'cores: %d\n' "$(nprocx)"

Show the count in the portable gnux shell

gnux shell -c 'nprocx --all'

Options

Each option has a page of its own, with its usage and examples.

  • --all: Print the number of installed processors.
  • --ignore: If possible, exclude N processing units.

Exit status

StatusMeaning
0success: every operand was processed
1failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake

Help in the terminal

nprocx --help                # options, first examples, exit status
gnux help nprocx             # this page: the full manual
gnux help nprocx --OPTION    # one option in depth, with its examples

See also

On this page