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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
export CPU_CORES=$(grep -c ^processor /proc/cpuinfo)
export CPU_CORES=$(grep -c ^processor /proc/cpuinfo)
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
export CPU_CORES=$(sysctl -a | grep machdep.cpu.core_count: | tr -d 'machdep.cpu.core_count: ')
export CPU_CORES=$(sysctl -a | grep machdep.cpu.core_count: | tr -d 'machdep.cpu.core_count: ')
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
export RAM_MB=$(( $(awk '/MemTotal/ {print $2}' /proc/meminfo) / 1024 ))
export RAM_MB=$(( $(awk '/MemTotal/ {print $2}' /proc/meminfo) / 1024 ))
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
export RAM_MB=$(($(sysctl -a | grep hw.memsize: | tr -d 'hw.memsize: ') / 1024 / 1024))
export RAM_MB=$(($(sysctl -a | grep hw.memsize: | tr -d 'hw.memsize: ') / 1024 / 1024))
export RAM_MB=$(($(sysctl -a | grep  hw.memsize: |  tr -d 'hw.memsize: ') / 1024 / 1024))