gpt4 book ai didi

linux - 脚本导致 CPU 使用率高

转载 作者:太空宇宙 更新时间:2023-11-04 04:45:51 25 4
gpt4 key购买 nike

我为一个 friend 编写了这个脚本,以便在他的屏幕上显示信息,但是每当他运行它时,他的 CPU 负载就会上升到接近 80%,并且变得非常热。不知道是不是他的电脑设置有问题,还是我这个bash写的效率很低。

#!/bin/sh

# Include config file.
. ~/.config/lemonbar/.config

# Fetch current network connection state -- and SSID, if available.
network(){
networkState=$(grep -R "up" /sys/class/net/*/operstate)
networkSSID=$(iwgetid -r)

if [[ ${networkState} != "" && ${networkSSID} != "" ]]; then
network=${networkSSID}
networkColor=${foreground}
elif [[ ${networkState} != "" ]]; then
network="Online"
networkColor=${foreground}
else
network="Offline"
networkColor=${red}
fi

echo "%{F$networkColor} ${network}"
}

# Fetch current dropbox sync status.
dropbox(){
dropboxInfo=$(dropbox-cli status)

if [[ $dropboxInfo == "Dropbox isn't running!" ]]; then
dropbox="Not Running"
dropboxColor=${red}
elif [[ $dropboxInfo == "Connecting..." || $dropboxInfo == "Starting..." ]]; then
dropbox="Connecting"
dropboxColor=${foreground}
elif [[ $(echo $dropboxInfo | grep "Syncing") != "" || $(echo $dropboxInfo | grep "Downloading") != "" || $(echo $dropboxInfo | grep "Indexing") != "" ]]; then
dropbox="Syncing"
dropboxColor=${green}
else
dropbox="Idle"
dropboxColor=${foreground}
fi

echo "%{F$dropboxColor} ${dropbox}"
}

# Fetch current volume state and mute state.
volume(){
volumeState=$(ponymix get-volume)

# Determine volume icon depending on the level of volume.
if [[ ${volumeState} -eq 0 ]]; then
volumeIcon=""
elif [[ ${volumeState} -le 50 ]]; then
volumeIcon=""
else
volumeIcon=""
fi

if $(ponymix is-muted); then
volumeColor=${red}
else
volumeColor=${foreground}
fi

echo "%{F$volumeColor}${volumeIcon} ${volumeState}%"
}

# Fetch the current percentage of the battery's capacity.
battery(){
if [[ -d /sys/class/power_supply/BAT0 ]]; then

batteryState=$(cat /sys/class/power_supply/BAT0/status)
batteryPower=$(cat /sys/class/power_supply/BAT0/capacity)

# Determine battery icon based on capacity and state.
if [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 20 ]]; then
batteryIcon=""
batteryColor=${red}
elif [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 40 ]]; then
batteryIcon=""
batteryColor=${foreground}
elif [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 60 ]]; then
batteryIcon=""
batteryColor=${foreground}
elif [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 80 ]]; then
batteryIcon=""
batteryColor=${foreground}
elif [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 100 ]]; then
batteryIcon=""
batteryColor=${foreground}
else
batteryIcon=""
batteryColor=${green}
fi
echo "%{F$batteryColor}${batteryIcon} ${batteryPower}%"
else
echo "%{F$red}No Battery Detected"
fi
}

# Fetch the current date.
calendar(){
calendar=$(date "+%A, %b %d, %Y")
echo "%{F$foreground} ${calendar}"
}

# Fetch the current time.
clock(){
clock=$(date "+%I:%M %p")
echo "%{F$foreground} ${clock}"
}

# Format selected blocks for piping into bar.
status(){
currentSeparator="${separator}"
numberOfBlocks=$((${#selectedBlocks[@]} - 1))

for ((i=0; i<=${numberOfBlocks}; i++)); do
if [[ ${i} -eq ${numberOfBlocks} ]]; then
currentSeparator=""
fi

status+="$(${selectedBlocks[i]})${currentSeparator}"
done

echo "${status}"
}

# Pipe functions to the bar infinitely.
while true; do
echo "%{c}$(status)"
done | lemonbar -g ${panelWidth}x${panelHeight}+${panelX}+${topPanelY} -f "${font}-${fontSize}" -f "${iconFont}" -B "${background}" -p -d | \
while true; do read line; eval $line; done &

配置文件:

#!/bin/sh

# Import config files.
. ~/.universal

# Select which blocks are displayed on the status bar.
selectedBlocks=(
"network"
"dropbox"
"volume"
# "battery"
"calendar"
"clock"
)

# Bar panel settings
separator="%{F${accent}} :: "

gapSize=16
borderSize=4

panelHeight=$((${gapSize} * 2))
panelWidth=$((${screenWidth} - ${panelHeight}))
panelX=${gapSize}
topPanelY=${gapSize}
bottomPanelY=$((${screenHeight} - ${gapSize} - ${panelHeight}))

最佳答案

您可能需要在交互之间添加暂停,否则它将尽可能快地运行所有内容并使用您的所有资源。这就是您所经历的。

要每两秒更新一次,您可以使用

sleep 2s

关于linux - 脚本导致 CPU 使用率高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37125995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com