简体   繁体   English

如何在bash /替换文本/ CPU使用中使函数在后台工作

[英]How to make a function work in background in bash / replace text / CPU usage

I have been playing around with the bashrc and one of the thing I want to see at all time is my cpu usage in percentage. 我一直在玩bashrc,我想要一直看到的一件事是我的cpu使用百分比。 I decided to set this data in my PS1. 我决定在我的PS1中设置这些数据。 The problem is that to have an accurate estimation of my CPU usage I need to do operations that require waiting for at least 0.5 seconds. 问题是要准确估计我的CPU使用率,我需要进行需要等待至少0.5秒的操作。

As a result of this, my new command line only displays at the end of the CPU calculation, 0.5 seconds later, which is really not acceptable. 因此,我的新命令行仅在CPU计算结束时显示,0.5秒后,这实际上是不可接受的。 To deal with this I thought that I could maybe use a thread to do the CPU calculation and only display it at the end but I don't know how to do so. 为了解决这个问题,我想我可以使用一个线程进行CPU计算,但最后只显示它,但我不知道该怎么做。

One of the problem is that I display other information after CPU percentage so I don't know if it is even possible to delay the CPU display while still displaying the rest of the command line. 问题之一是我在CPU百分比之后显示其他信息所以我不知道是否甚至可以在显示命令行的其余部分时延迟CPU显示。 I thought that maybe I could display a temporary string such as ??.?? 我想也许我可以显示一个临时字符串,如??.?? and then replace it by the real value but I am not sure how to do so since if I type commands fast the position of the ??.?? 然后用实际值替换它,但我不知道怎么做,因为如果我快速键入命令??.??的位置??.?? can change (for example typing ls 5 times in a row very fast). 可以改变(例如连续5次连续输入ls)。

Maybe there is an even simpler solution to my problem such as calculating the CPU percentage in an other way ? 也许有一个更简单的解决方案来解决我的问题,比如以另一种方式计算CPU百分比?

My CPU percentage calculating function: 我的CPU百分比计算功能:

function cpuf(){
    NonIdle=0;Idle=0;Total=0;TotalD=0;Idled=0
    NonIdle=$((`cat /proc/stat | awk '/^cpu / {print$2+$3+$4+$7+$8+$9}'` - $NonIdle))
    Idle=$((`cat /proc/stat | awk '/^cpu / {print$5+$6}'` - $Idle))
    sleep 0.5
    NonIdle=$((`cat /proc/stat | awk '/^cpu / {print$2+$3+$4+$7+$8+$9}'` - $NonIdle))
    Idle=$((`cat /proc/stat | awk '/^cpu / {print$5+$6}'` - $Idle))
    Total=$((Idle+NonIdle))
    CPU=$(((Total-Idle)/Total))
    echo `echo "scale=2;($Total*100-$Idle*100)/$Total" | bc -l`
}

How I call it in the bashrc: 我如何在bashrc中调用它:

alias cpu="cpuf"
PS1+="(\[${MAGENTA}\]CPU $(cpu)%"

There is no need to reinvent the wheel here, linux already takes care of capturing system stats in /proc/loadavg . 这里没有必要重新发明轮子,linux已经负责捕获/proc/loadavg系统统计信息。 The first number is average load in the last minute across all cpus, so we just need to divide by the number of cpus, which we can determine by reading /proc/cpuinfo . 第一个数字是所有cpu中最后一分钟的平均负载,所以我们只需要除以cpus的数量,我们可以通过读取/proc/cpuinfo来确定。 Rolling this into .bashrc we get: 将其转换为.bashrc我们得到:

.bashrc 的.bashrc

...
# My fancy prompt, adjust as you like...
FANCY_PROMPT="$GREEN\u$YELLOW@\h:$PURPLE\w$BLUE$ $RESET"

CPUS=$( grep -c bogomips /proc/cpuinfo )

_prompt_command() {
    LOAD_AVG_1_MIN=$( cut -d ' ' -f 1 /proc/loadavg )
    PERCENT=$( echo "scale=0; $LOAD_AVG_1_MIN * 100 / $CPUS" | bc -l )
    PS1="CPU $PERCENT% $FANCY_PROMPT"
    true
}

PROMPT_COMMAND="_prompt_command"

In Use: 正在使用:

在此输入图像描述

SO linux /proc/loadavg SO linux / proc / loadavg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM