grep -c ^processor /proc/cpuinfo 2>/dev/null for Linux systems, sysctl -n hw.ncpu for OS X  returns number of cores. Not sure about support for Illumos/Solaris.