Skip to content

Instantly share code, notes, and snippets.

@random-person-001
Created October 31, 2016 21:50
Show Gist options
  • Save random-person-001/da4ac5b54996ca3af6dba0f579b7ba5d to your computer and use it in GitHub Desktop.
Save random-person-001/da4ac5b54996ca3af6dba0f579b7ba5d to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Output the CPU usage of a specified PID every so often.
# function 'u' from StackExchange, rest by Riley Hunt.
#
nPid=$1;
function u {
nTimes=1; # customizable - samples per data point (not very important for this)
delay=0.1; # customizable - interval between samples (not very important for this)
strCalc=`top -d $delay -b -n $nTimes -p $nPid \
|grep $nPid \
|sed -r -e "s;\s\s*; ;g" -e "s;^ *;;" \
|cut -d' ' -f9 \
|tr '\n' '+' \
|sed -r -e "s;(.*)[+]$;\1;" -e "s/.*/scale=2;(&)\/$nTimes/"`;
nPercCpu=`echo "$strCalc" |bc -l` # fancy formatting stuff to get what's in the CPU column of the TOP output
echo $nPercCpu
}
for i in {1..500} # just press ctrl+c to interrupt when you feel like it
do
echo `u` # print cpu usage of process (which is the output of the function 'u')
sleep 4 # customizable - seconds to sleep between each data point
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment