Skip to content

Instantly share code, notes, and snippets.

@gtrabanco
Created September 15, 2020 00:12
Show Gist options
  • Save gtrabanco/4b9454cb2ce4e2a3afd06bd386b9e8e4 to your computer and use it in GitHub Desktop.
Save gtrabanco/4b9454cb2ce4e2a3afd06bd386b9e8e4 to your computer and use it in GitHub Desktop.
Get some hardware and running process information
#!/bin/bash
#Number of cores
cores=$(grep -c ^processor /proc/cpuinfo) # Count all lines that starts with processor
print "This system has ${cores} cores"
nproc --all # This will also show a number
grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}' # Hyper-Threading
cat /proc/cpuinfo # Human readable information about all the processors
# The best way is using
getconf _NPROCESSORS_ONLN # In some other *NIX could be: getconf NPROCESSORS_ONLN
# Also a good way is
lscpu -p
##### Script to know number of physical and logical processors in any *NIX system #####
# https://stackoverflow.com/questions/6481005/how-to-obtain-the-number-of-cpus-cores-in-linux-from-the-command-line/23378780#23378780
#######################################################################################
# macOS: Use `sysctl -n hw.*cpu_max`, which returns the values of
# interest directly.
# CAVEAT: Using the "_max" key suffixes means that the *maximum*
# available number of CPUs is reported, whereas the
# current power-management mode could make *fewer* CPUs
# available; dropping the "_max" suffix would report the
# number of *currently* available ones; see [1] below.
#
# Linux: Parse output from `lscpu -p`, where each output line represents
# a distinct (logical) CPU.
# Note: Newer versions of `lscpu` support more flexible output
# formats, but we stick with the parseable legacy format
# generated by `-p` to support older distros, too.
# `-p` reports *online* CPUs only - i.e., on hot-pluggable
# systems, currently disabled (offline) CPUs are NOT
# reported.
# Number of LOGICAL CPUs (includes those reported by hyper-threading cores)
# Linux: Simply count the number of (non-comment) output lines from `lscpu -p`,
# which tells us the number of *logical* CPUs.
logicalCpuCount=$([ $(uname) = 'Darwin' ] &&
sysctl -n hw.logicalcpu_max ||
lscpu -p | egrep -v '^#' | wc -l)
# Number of PHYSICAL CPUs (cores).
# Linux: The 2nd column contains the core ID, with each core ID having 1 or
# - in the case of hyperthreading - more logical CPUs.
# Counting the *unique* cores across lines tells us the
# number of *physical* CPUs (cores).
physicalCpuCount=$([ $(uname) = 'Darwin' ] &&
sysctl -n hw.physicalcpu_max ||
lscpu -p | egrep -v '^#' | sort -u -t, -k 2,4 | wc -l)
# Print the values.
cat <<EOF
# of logical CPUs: $logicalCpuCount
# of physical CPUS: $physicalCpuCount
EOF
#### End of script to know the number of CPUs ####
#######################################################################################
# Uptime with load average
uptime
# Top processes sorted by RAM and CPu
# https://www.tecmint.com/find-linux-processes-memory-ram-cpu-usage/
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
# All processes sorted by RAM and CPU
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem
# Freespace by mountpoint
df -h
# Calculate the size of a folder
du -hsc /home
# Calculate the size of each file or folder in a folder
du -hsc /etc/*
# Free memory
free -m
# More memory stats
vmstat -a -s
# Realtime stats
top
htop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment