AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1322488
Accepted
Cas
Cas
Asked: 2021-03-11 00:56:09 +0800 CST2021-03-11 00:56:09 +0800 CST 2021-03-11 00:56:09 +0800 CST

如何“超时”一个函数,使其每 5 秒只运行一次,即使它每秒被调用多次

  • 772

见下文:

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 语句为真,它会显示一条消息。每次在脚本中执行命令时(由于在 trap 命令中使用了 DEBUG),它都会检查 api(请参阅监视变量)。我的脚本非常大(2800 行),它每秒运行多次通知函数(有时每秒 10 次)。这使得我的脚本非常慢(由于 api 请求的数量),有时甚至无法使用,而且该函数甚至不必运行那么多次。

我想要它,这样一旦函数运行,它就不能在接下来的 5 秒内运行,无论它被陷阱命令调用多少次。就像函数的睡眠/超时。我尝试了以下但没有奏效:

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
command-line
  • 1 1 个回答
  • 87 Views

1 个回答

  • Voted
  1. Best Answer
    user986805
    2021-03-11T10:15:37+08:002021-03-11T10:15:37+08:00

    您可以创建一个中间函数来跟踪时间。如果 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..
    
    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve