Skip to content

Instantly share code, notes, and snippets.

@quidmonkey
Last active December 17, 2015 21:08
Show Gist options
  • Save quidmonkey/5672094 to your computer and use it in GitHub Desktop.
Save quidmonkey/5672094 to your computer and use it in GitHub Desktop.
Bash Shell Script: Print CPU Usage for a Given Program to a Terminal
# !/bin/bash
while :; do
# grab process id for given arg
PID=$(ps aux | grep $1 | grep -v grep | grep -v cpu.sh | awk '{print $2}')
# grab cpu usage
USED=$(ps -p $PID -o %cpu)
# grab arg string length + 9
LEN=$(expr $(echo $1 | awk '{print length}') + 9)
# hack! print backspace mulitple times to erase previous input
for (( i=1 ; i<=$LEN ; i++ )); do
echo \\b\\c;
done
# echo cpu usage
echo $1 $USED\\c
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment