OSX

CPU & RAM data in your OS X Terminal

Today, I blindly ran a script designed for ubuntu. When it failed, I came across the following line

export CPU_CORES=$(grep -c ^processor /proc/cpuinfo)

On a Linux distro, /proc/cpuinfo provides information on the processor your computer is running. This file is not present in OS X. Instead we’ll need to use sysctl to get the information. The following command will give you the equivalent data on your mac.

export CPU_CORES=$(sysctl -a | grep  machdep.cpu.core_count: |  tr -d 'machdep.cpu.core_count: ')

Bonus: We have to do some similar changes to get RAM information. On Linux, we can pull from /proc/meminfo

export RAM_MB=$(( $(awk '/MemTotal/ {print $2}' /proc/meminfo) / 1024 ))

On OS X, we can go back to sysctl, and look for a different key. Note the double parentheses $(()) syntax for the math around this one.

export RAM_MB=$(($(sysctl -a | grep  hw.memsize: |  tr -d 'hw.memsize: ') / 1024 / 1024))