Michael Kelley
Michael Kelley

Reputation: 3689

How do I get the # of logical CPUs in zsh or bash?

How do I get the # of logical CPUs in zsh or bash? I want to create a makep alias that passes the # of cpus to 'make -j'.

Upvotes: 1

Views: 946

Answers (3)

Matheus Torquato
Matheus Torquato

Reputation: 1629

On macOS you can do:

sysctl -n hw.ncpu

Upvotes: 0

Sven
Sven

Reputation: 55

The simpelst solution is to call "nproc" which just returns the number of logical cores

nproc --all give you all installed processors (on openvz this returns the cpus of the system, not of the maybe limited number in your container!)

nproc without any option gives you the number of cores available to the current process (so bash or zsh in your case) nproc is part of coreutils.

No need to grep :)

For more info see "man nproc"

HTH

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 360305

For Linux:

grep -c proc /proc/cpuinfo

The shell doesn't matter. This will work in any.

Upvotes: 4

Related Questions