How to Determine the Current CPU Utilization from the Shell

How can I determine the current CPU utilization from the shell?

Linux does not have any system variables that give the current CPU utilization. Instead, you have to read /proc/stat several times: each column in the cpu(n) lines gives the total CPU time, and you have to take subsequent readings of it to get percentages. See this document to find out what the various columns mean.

Retrieve CPU usage and memory usage of a single process on Linux?


ps -p <pid> -o %cpu,%mem,cmd

(You can leave off "cmd" but that might be helpful in debugging).

Note that this gives average CPU usage of the process over the time it has been running.

Get CPU usage in shell script?

Use top -b (and other switches if you want different outputs). It will just dump to stdout instead of jumping into a curses window.

Retrieve CPU usage percentage

A simple awk could help you here(considering that you want to print only the numbers of sy column).

vmstat 1 10 | awk 'FNR>1{print $(NF-3)}'

NOTE: I have used vmstat 1 10 to perform 10 times vmstat command on server and then I am printing the $(NF-3) value which is 4th value from last.

How to get percentage of processor use with bash?

Processor use or utilization is a measurement over time. One way to measure utilization in % is by computation over two successive reads of /proc/stat. A simple common bash script to compute the percentage is:

#!/bin/bash

# Read /proc/stat file (for first datapoint)
read cpu user nice system idle iowait irq softirq steal guest< /proc/stat

# compute active and total utilizations
cpu_active_prev=$((user+system+nice+softirq+steal))
cpu_total_prev=$((user+system+nice+softirq+steal+idle+iowait))

usleep 50000

# Read /proc/stat file (for second datapoint)
read cpu user nice system idle iowait irq softirq steal guest< /proc/stat

# compute active and total utilizations
cpu_active_cur=$((user+system+nice+softirq+steal))
cpu_total_cur=$((user+system+nice+softirq+steal+idle+iowait))

# compute CPU utilization (%)
cpu_util=$((100*( cpu_active_cur-cpu_active_prev ) / (cpu_total_cur-cpu_total_prev) ))

printf " Current CPU Utilization : %s\n" "$cpu_util"

exit 0

use/output:

$ bash procstat-cpu.sh
Current CPU Utilization : 10

output over 5 iterations:

$ ( declare -i cnt=0; while [ "$cnt" -lt 5 ]; do bash procstat-cpu.sh; ((cnt++)); done )
Current CPU Utilization : 20
Current CPU Utilization : 18
Current CPU Utilization : 18
Current CPU Utilization : 18
Current CPU Utilization : 18

Getting CPU usage of a specific process using APPLESCRIPT

You do not need to use the fully qualified pathname of the executable in this use case as both ps and awk are within the PATH passed to the do shell script command, i.e.: /usr/bin:/bin:/usr/sbin:/sbin

Also, you do not need to execute the do shell script command twice. Just set the value of test prior to the repeat loop, and use as integer on the result of the do shell script command e.g.:

getProcessPercentCPU("Google Chrome")

on getProcessPercentCPU(someProcess)

set test to 0

repeat while test < 50
set test to ¬
(do shell script "ps -xco %cpu,command | awk '/" & someProcess & "$/ {print $1}'") ¬
as integer
end repeat

display dialog test

end getProcessPercentCPU

That said, using a loop like this can become resource intensive, so you might consider adding a delay command inside of the loop so the do shell script command isn't triggered constantly one iteration directly after the other. Additionally, consider using some method to escape the loop after a given period of time.

With a delay and timeout added:

getProcessPercentCPU("Google Chrome")

on getProcessPercentCPU(someProcess)

set i to 0
set test to 0

repeat while test < 50
set test to ¬
(do shell script "ps -xco %cpu,command | awk '/" & someProcess & "$/ {print $1}'") ¬
as integer
delay 2
set i to i + 1
if i ≥ 10 then exit repeat
end repeat

display dialog test

end getProcessPercentCPU


Related Topics



Leave a reply



Submit