简体   繁体   中英

Script causing high CPU usage

I wrote this script for a friend in order to display information on his screen, but whenever he runs it, his CPU load goes up to nearly 80% and it gets very hot. I do not know if it is a problem with how his computer is set up, or if I just wrote this bash very inefficiently.

#!/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 &

Config file:

#!/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}))

You are not giving the CPU any time to sleep. Your while loops are way too aggressive. You should specify an interval in the while loop, and give time back to the processor by sleep or usleep in between loop iterations.

You might want to add a pause between interations, otherwise it'll consistently run everything as fast as possible and use all of your resources. Which is what you're experiencing.

To update every two seconds you can use

sleep 2s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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