见下文:
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
您可以创建一个中间函数来跟踪时间。如果 rlimit 超过一秒,则将 epoch 重新设置为全局范围内的 rlimit 变量。