簡體   English   中英

在 Raspberry Pi 4 (Ubuntu 20.04.1) 中運行自定義 bash 腳本以控制 GPIO 的 CPU 使用率高

[英]High CPU usage running a custom bash script in Raspberry Pi 4 (Ubuntu 20.04.1) to control GPIO

我編寫了一個 bash 腳本來打開/關閉 GPIO 以控制風扇,但它導致 CPU 使用率高,我不知道為什么。

它可以工作,但是每當它從關閉狀態變為開啟狀態或反之亦然時,腳本會凍結,導致 CPU 使用率過高,大約 5 分鍾后,它會更改狀態並且 CPU 使用率恢復正常。 大約 20-60 秒后問題再次出現。

有人可以幫助我了解我的腳本有什么問題嗎?

[樹莓派 4 運行 Ubuntu 20.04]

#!/bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"

gpio -g mode 3 out

on=48000
off=44000

while true; do
    cpu=$(</sys/class/thermal/thermal_zone0/temp)    # get CPU temperature

    if (( "$cpu" > "$on" )); then
        gpio -g write 3 1    # turn fan ON
        echo "CPU Hot"
        sleep 60
    fi

    if (( "$off" > "$cpu" )); then
        echo "CPU Cool."
        gpio -g write 3 0    # turn fan OFF
        sleep 5
    fi
done

好的,所以我能夠解決問題,感謝@shellter ...

問題是,當CPU溫度在48004400之間時,腳本沒有任何condition處理

該解決方案的基本思想是:

if (CPU is Hot); then
    turn fan ON
    set boolean = true
    sleep 60 sec

else if (CPU is Cool); then
    turn fan OFF
    set boolean = false
    sleep 5 sec

else (when CPU is neither Hot OR Cool.. somewhere between 4400 && 4800)

    if (boolean is true.. meaning CPU didn't go below 4400); then
        sleep 60 sec

    else (boolean is false.. meaning CPU didn't go above 4800); then
        sleep 5 sec 

工作代碼是:

#!/bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"

gpio -g mode 3 out

on=48000
off=44000
hot=false

while true; do
    cpu=$(</sys/class/thermal/thermal_zone0/temp)

    if (( "$cpu" > "$on" )); then
        echo "CPU Hot"
        hot=true
        gpio -g write 3 1    # turn fan ON
        sleep 60

    elif (( "$off" > "$cpu" )); then
        echo "CPU Cool."
        hot=false
        gpio -g write 3 0    # turn fan OFF
        sleep 5

    else
        if [ "$hot" = true ]; then
            echo "CPU still Hot"
            sleep 60
        else
            echo "CPU Cool"
            sleep 5
        fi
    fi
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM