adammcintosh adammcintosh - 1 year ago 58
Linux Question

How to get CPU Utilization number from top?

How would I get the number for CPU Utilization from running the top command on a specific user?


Pipe top to awk and add up the CPU utilization column:

top -b -n 1 -u username | awk 'NR>7 { sum += $9; } END { print sum; }'

That awk command says "For rows (from top) greater than row 7, add up the number in field 9 $9 and store it in variable sum. Once you have gone through all of the rows in the piped top command, print out the value in variable sum".