linux - Extracting certain information from htop command - Stack Overflow

I want to extract the highest CPU load, the process ID causing the high CPU problem and the app name fr

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?

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?

Share Improve this question asked Mar 10 at 13:01 iso_9001_iso_9001_ 2,8917 gold badges34 silver badges48 bronze badges 3
  • Don't use htop for this kind of thing. stackoverflow/questions/17534591/… – tink Commented Mar 10 at 17:19
  • @tink I don't have to use htop. I'm OK with any other commands as long as providing valid output. – iso_9001_ Commented Mar 10 at 20:18
  • - does the edited answer fit your requirements? – tink Commented Mar 11 at 22:14
Add a comment  | 

1 Answer 1

Reset to default 1

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=""}
  1. Hint: There's a reason that images of text are discouraged

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744845583a4596814.html

相关推荐

  • linux - Extracting certain information from htop command - Stack Overflow

    I want to extract the highest CPU load, the process ID causing the high CPU problem and the app name fr

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信