如何使某个函数“超时”,使其每 5 秒仅运行一次,尽管每秒调用多次

如何使某个函数“超时”,使其每 5 秒仅运行一次,尽管每秒调用多次

见下文:

still_notify=false
has_been_notified=false
notify()
{
        $has_been_notified && return
        watching=$(if [[ $(GET http://$ipplexserver:32400/status/sessions?X-Plex-Token=$plexapitoken | grep -o "^</Video>$") ]]; then echo true; else echo false; fi)
        if [[ $watching = false ]]
        then
                clear
                echo "Updating notification"
                echo "--------------------------------"
                echo "Nobody is watching media on your plex server anymore"
                echo "You can now update plex"
                echo "-----"
                echo "You see this message because you chose to be notified when you were trying to update plex"
                echo "--------------------------------"
                sed -i "s|^still_notify=true$|still_notify=false|" $filelocation/advancedplexapi.sh
                read -rp "continue | update: " notifyoption
                if [[ ${notifyoption,,} = "continue" ]]
                then
                        echo "this is to exit out of the if statement" >> /dev/null

                elif [[ ${notifyoption,,} = update ]]
                then
                        option=update
                fi
                has_been_notified=true
        fi
}

if [[ $still_notify = true ]]
then
        trap notify DEBUG
fi

if [[ if-statement ]]
then
       sed -i "s|^still_notify=false$|still_notify=true|" $filelocation/advancedplexapi.sh
       trap notify DEBUG
fi

这是我的脚本中的通知系统。当运行底部的 if 语句时,脚本将继续运行,但在后台,它会检查 plex api,如果 if 语句为真,它会显示一条消息。每次在脚本中执行命令时,它都会检查 api(查看监视变量)(因为 trap 命令中使用了 DEBUG)。我的脚本非常大(2800 行),以至于它每秒运行多次通知函数(有时每秒 10 次)。这使我的脚本非常慢(由于 api 请求的数量),有时甚至无法使用,而该函数甚至不必运行那么多次。

我希望一旦该函数运行,接下来的 5 秒内它就无法运行,无论 trap 命令调用它多少次。就像函数的睡眠/超时一样。我尝试了以下方法,但没有奏效:

timeout=false
still_notify=false
has_been_notified=false
notify()
{
        if [[ $timeout = false ]]
        then
        sed -i "s|^timeout=false$|timeout=true|" $filelocation/advancedplexapi.sh
        $has_been_notified && return
        watching=$(if [[ $(GET http://$ipplexserver:32400/status/sessions?X-Plex-Token=$plexapitoken | grep -o "^</Video>$") ]]; then echo true; else echo false; fi)
        if [[ $watching = false ]]
        then
                clear
                echo "Updating notification"
                echo "--------------------------------"
                echo "Nobody is watching media on your plex server anymore"
                echo "You can now update plex"
                echo "-----"
                echo "You see this message because you chose to be notified when you were trying to update plex"
                echo "--------------------------------"
                sed -i "s|^still_notify=true$|still_notify=false|" $filelocation/advancedplexapi.sh
                read -rp "continue | update: " notifyoption
                if [[ ${notifyoption,,} = "continue" ]]
                then
                        echo "this is to exit out of the if statement" >> /dev/null

                elif [[ ${notifyoption,,} = update ]]
                then
                        option=update
                fi
                has_been_notified=true
        fi
}

while true
do
        sleep 5s
        sed -i "s|^timeout=true$|timeout=false|" $filelocation/advancedplexapi.sh
done &

if [[ $still_notify = true ]]
then
        trap notify DEBUG
fi

if [[ if-statement ]]
then
       sed -i "s|^still_notify=false$|still_notify=true|" $filelocation/advancedplexapi.sh
       trap notify DEBUG
fi

答案1

您可以创建一个中间函数来跟踪时间。如果 rlimit 超过一秒,则将 epoch 重新设置为全局范围内的 rlimit 变量。

#!/bin/bash

debug_func(){
    local -n a=rlimit
    ((($EPOCHSECONDS - a) > 1)) && \
        { a=$EPOCHSECONDS; notify; }
}
trap debug_func DEBUG

notify(){
    echo "Ding Dong!"
}

echo  1..
echo  2..
echo  3..
sleep 2
echo  4..

相关内容