I'm running this command
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
Yet it only outputs something like 0.99xxxx%
If I do an apt-get upgrade or any process, I would imagine it would go above 1%. Even running stress -c 1 doesn't make it change any.
Is there a way to log CPU usage accurately? Server has 1 vCPU.
Need to have this log every 5 seconds.
while sleep 5; do "code" >> logfile; done
If you want to log the highest cpu percentage (this is, the process with the highest cpu usage in the moment of querying), you can use:
Explanation of the command:
ps -e -o %cpugives you process information of every process in the output format that consist only in the cpu usage percentagetail -n +2filters the previous output starting from the second line (thus ignoring the header printed byps)sort -rsort the values in reverse order (highest first)head -n 1filters the data returned bysortso you discard all but the first line