CPU Usage from Linux then using it in a arithmetic expression
Bash arithmetic is integer only. It won't accept fractional numbers like 93.4.
You need to pipe the expression through bc
.
joulesFinal=`echo $joules2 * $cpu | bc`
How to calculate sum of CPU usage of users who are not me
Here's a command that should do the trick:
ps ax -o pcpu:5,user --no-headers | tr -s ' ' | grep -v $(whoami) | cut -d' ' -f2 | (tr '\n' + ; echo 0; ) | bc
The ps
command will list the CPU usages of every process alongside its owner. The tr
will squeeze together multiple spaces, so the cut
later on works as wanted. The grep
will filter out processes owned by you. The cut
command will select the first column, ie, the CPU usages. The tr
will substitute new lines for plus signs, and finally bc
will evaluate the resulting arithmetic expression.
Cheers.
Bash script checking cpu usage of specific process
The problem is that bash can't handle decimals. You can just multiply them by 100 and work with plain integers instead:
#!/bin/bash
declare -i app_pid
declare -i app_cpu
declare -i cpu_limit
app_name="top"
cpu_limit="5000"
app_pid=`ps aux | grep $app_name | grep -v grep | awk {'print $2'}`
app_cpu=`ps aux | grep $app_name | grep -v grep | awk {'print $3*100'}`
if [[ $app_cpu -gt $cpu_limit ]]; then
echo "crap"
else
echo "we're good"
fi
Keep in mind that CPU percentage is a suboptimal measurement of application health. If you have two processes running infinite loops on a single core system, no other application of the same priority will ever go over 33%, even if they're trashing around.
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
Bash: wait until CPU usage gets below a threshold
wait_for_cpu_usage()
{
current=$(mpstat 1 1 | awk '$12 ~ /[0-9.]+/ { print int(100 - $12 + 0.5) }')
while [[ "$current" -ge "$1" ]]; do
current=$(mpstat 1 1 | awk '$12 ~ /[0-9.]+/ { print int(100 - $12 + 0.5) }')
sleep 1
done
}
Notice it requires sysstat package installed.
How to obtain the number of CPUs/cores in Linux from the command line?
grep -c ^processor /proc/cpuinfo
will count the number of lines starting with "processor" in /proc/cpuinfo
For systems with hyper-threading, you can use
grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}'
which should return (for example) 8
(whereas the command above would return 16
)
Related Topics
Cannot Find Module 'Firebase-Admin' When Trying to Deploy Firebase Functions
Shell Script Not Running via Crontab, Runs Fine Manually
Do I Get a Notification from Epoll When a Fd Is Closed
Suppress Error Message While Using Cat Command
How to Run Cassandra (Cqlsh) from Anywhere
Error When Compiling Linux Kernel 3.2 for Arm
How to Check If Emacs in Frame or in Terminal
How to Automatically Close The Execution of The 'Qemu' After End of Process
Succinct Way to Print All Lines Up Until The Last Line That Matches a Given Pattern
Using Ebpf to Measure CPU Mode Switch Overhead Incured by Making System Call
Floating Point Rounding in Shell
What Is Inside So File of Python Library Distribution
How to Access Files in Hadoop Hdfs
What Is How to Call Execve with Arguments in Assembly
Shell Must Parse Ls -Al Output and Get Last Field (File or Directory Name) Any Solution