I want to extract the highest CPU load
, the process ID
causing the high CPU problem and the app name
from the htop
command. Please take a look at this screenshot.
The first rectangle on the left is to get the PID
of the highest CPU load process.
The one in the middle is the CPU
load. I need this to compare with a threshold value.
The third one is the specific server causing this problem. I need 204-8204
information to kill that instance.
I got some example commands from chatGPT
but it's always working. For instance, it gives me 345 where the actual load is over 3000. Since the threshold is much higher than 345, I cannot kill the process.
How can I solve this?
On my machine (not as many cores as yours) this works, the first line of process data sits on line number 8, hence the NR==8
in the awk command. The little for-loop joins all fields from # 13 (beginning of command) to NF (number of fields in line). Top is instructed to sort by %CPU, output the full command rather than the process name and output one line in batch mode.
top -o "%CPU" -c -b -n 1 | awk 'NR==8{for(i=13;i<=NF;i++){command = command" "$i}; printf "%s\t%s\t%s\n",$1,$9,command}'
2555274 29.4 top -o %CPU -c -b -n 1
I don't have sample process names that match yours and have no intention to type all that ... so the parsing out of your [Server-mumble]1 is up to you.
P.S.: I adjusted the awk to cater to your special needs, extracting the numeric substring from your -D
parameter ...
top -o "%CPU" -c -b -n 1 |awk 'BEGIN{p=0} $1=="PID"{p=1;next} {if(p==1){command=gensub(/^.*-D\[Server:([0-9-]+).*/,"\\1","1"); printf "%s\t%s\t%s\n",$1,$9,command}p=0;command=""}