我为朋友写了这个脚本,以便在他的屏幕上显示信息,但每当他运行它时,他的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}))
答案 0 :(得分:1)
您没有给CPU任何时间睡觉。你的while循环过于激进了。您应该在while循环中指定一个间隔,并在循环迭代之间通过sleep
或usleep
将时间返回给处理器。
答案 1 :(得分:1)
您可能希望在交互之间添加暂停,否则它将始终以尽可能快的速度运行并使用您的所有资源。这就是你所经历的。
要每两秒更新一次,您可以使用
sleep 2s