Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
# calculate the cpu usage of a single process
# jvehent oct.2013
[ -z $1 ] && echo "usage: $0 <pid>"
sfile=/proc/$1/stat
if [ ! -r $sfile ]; then echo "pid $1 not found in /proc" ; exit 1; fi
proctime=$(cat $sfile|awk '{print $14}')
totaltime=$(grep '^cpu ' /proc/stat |awk '{sum=$2+$3+$4+$5+$6+$7+$8+$9+$10; print sum}')
echo "time ratio cpu%"
while [ 1 ]; do
sleep 1
prevproctime=$proctime
prevtotaltime=$totaltime
proctime=$(cat $sfile|awk '{print $14}')
totaltime=$(grep '^cpu ' /proc/stat |awk '{sum=$2+$3+$4+$5+$6+$7+$8+$9+$10; print sum}')
ratio=$(echo "scale=2;($proctime - $prevproctime) / ($totaltime - $prevtotaltime)"|bc -l)
echo "$(date --rfc-3339=seconds); $ratio; $(echo "$ratio*100"|bc -l)"
done
@tobiaspal
Copy link

tobiaspal commented Nov 30, 2013

You can use simply:
while true; do
instead of
while [ 1 ]; do

@terryburton
Copy link

terryburton commented Mar 19, 2014

while true is more costly since it executes a new process, /bin/true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment