Shell 限制Linux命令每单位时间执行次数的方法

Shell 限制Linux命令每单位时间执行次数的方法,shell,time,limit,rate,Shell,Time,Limit,Rate,我想限制在给定时间段内执行命令的次数。我知道一种方法可以做到这一点,但我的方法并不整洁,我希望有更好的方法来实现这一点的建议。具体来说,我要处理的场景如下: 我正在使用程序Motion监视和录制网络摄像头中的图像。当检测到运动时,程序保存图像并执行命令。我希望它执行的命令之一是一个简单的命令,用于在检测到运动时向我发送电子邮件。出现了一个困难,因为该命令每秒可能执行多次。这会很快导致在很短的时间内发送数千封电子邮件。我想我需要的是如下程序: on motion detected Has it

我想限制在给定时间段内执行命令的次数。我知道一种方法可以做到这一点,但我的方法并不整洁,我希望有更好的方法来实现这一点的建议。具体来说,我要处理的场景如下:

我正在使用程序Motion监视和录制网络摄像头中的图像。当检测到运动时,程序保存图像并执行命令。我希望它执行的命令之一是一个简单的命令,用于在检测到运动时向我发送电子邮件。出现了一个困难,因为该命令每秒可能执行多次。这会很快导致在很短的时间内发送数千封电子邮件。我想我需要的是如下程序:

on motion detected
 Has it been more than 1 minute since motion was last detected?
  If it has, send a notification e-mail.
  If it has not, don't send a notification e-mail.
我想用一个简洁的命令来结束这个过程。我目前的做法是保存一个临时文件,我怀疑这不是最整洁的做法


谢谢你的任何想法

传统方法是创建一个文件,使用它在其内容中或通过其元数据(
mtime
e.t.c.)存储时间戳。在进程之外没有其他标准的方法来保持持久的信息——我假设你会考虑数据库,这样会被过度使用。 但是,如果调用方(例如
运动
)阻塞等待您的进程完成,则可能会有另一种选择。在这种情况下,您的脚本可能如下所示:

#!/bin/sh

echo "The Martians are coming!" | mail -s "Invasion" user@example.com

sleep 60

最后一行确保等待此脚本终止的任何调用方都必须等待至少60秒,这将施加最大速率限制。

传统方法是创建一个文件,使用它在其内容中或通过其元数据(
mtime
e.t.c.)存储时间戳。在进程之外没有其他标准的方法来保持持久的信息——我假设你会考虑数据库,这样会被过度使用。 但是,如果调用方(例如
运动
)阻塞等待您的进程完成,则可能会有另一种选择。在这种情况下,您的脚本可能如下所示:

#!/bin/sh

echo "The Martians are coming!" | mail -s "Invasion" user@example.com

sleep 60

最后一行确保等待此脚本终止的任何调用方都必须等待至少60秒,这将施加一个最大速率限制。

好吧,下面是每次检测到运动时运行的脚本类型:

#!/bin/bash
    #user variables #userSet
        timeIntervalInSecondsBetweenCommandExecutions=120
        lastExecutionTimeFileName="last_command_execution_time.txt"
        command=$(cat << 2012-08-20T1654
twidge update "@<Twitter account> motion detected $(date "+%Y-%m-%dT%H%M")";
echo "motion detected" | festival --tts
2012-08-20T1654
)
    # Get the current time.
        currentTimeEpoch="$(date +%s)"
    # Check if the last execution time file exists. If it does not exist, run the command. If it does exist, check the time stored in it against the current time.
        if [ -e ${lastExecutionTimeFileName} ]; then
            lastCommandExecutionTimeEpoch="$(cat "${lastExecutionTimeFileName}")"
            timeInSecondsSinceLastCommandExecution="$(echo "${currentTimeEpoch}-${lastCommandExecutionTimeEpoch}" | bc)"
            # If the time since the last execution is greater than the time interval between command executions, execute the command and save the current time to the last execution time file.
                if [ ${timeInSecondsSinceLastCommandExecution} -ge ${timeIntervalInSecondsBetweenCommandExecutions} ]; then
                    eval ${command}
                    echo "${currentTimeEpoch}" > "${lastExecutionTimeFileName}"
                fi
        else
            eval ${command}
        fi
#/bin/bash
#用户变量#用户集
命令执行之间的时间间隔秒=120
lastExecutionTimeFileName=“last\u command\u execution\u time.txt”
命令=$(cat“${lastExecutionTimeFileName}”
fi
其他的
eval${command}
fi

简而言之,它使用一个文件来记住上次运行的时间。因此,这是一个答案,但我仍然认为它不雅观。

好吧,下面是每次检测到运动时运行的脚本类型:

#!/bin/bash
    #user variables #userSet
        timeIntervalInSecondsBetweenCommandExecutions=120
        lastExecutionTimeFileName="last_command_execution_time.txt"
        command=$(cat << 2012-08-20T1654
twidge update "@<Twitter account> motion detected $(date "+%Y-%m-%dT%H%M")";
echo "motion detected" | festival --tts
2012-08-20T1654
)
    # Get the current time.
        currentTimeEpoch="$(date +%s)"
    # Check if the last execution time file exists. If it does not exist, run the command. If it does exist, check the time stored in it against the current time.
        if [ -e ${lastExecutionTimeFileName} ]; then
            lastCommandExecutionTimeEpoch="$(cat "${lastExecutionTimeFileName}")"
            timeInSecondsSinceLastCommandExecution="$(echo "${currentTimeEpoch}-${lastCommandExecutionTimeEpoch}" | bc)"
            # If the time since the last execution is greater than the time interval between command executions, execute the command and save the current time to the last execution time file.
                if [ ${timeInSecondsSinceLastCommandExecution} -ge ${timeIntervalInSecondsBetweenCommandExecutions} ]; then
                    eval ${command}
                    echo "${currentTimeEpoch}" > "${lastExecutionTimeFileName}"
                fi
        else
            eval ${command}
        fi
!/bin/bash
#用户变量#用户集
命令执行之间的时间间隔秒=120
lastExecutionTimeFileName=“last\u command\u execution\u time.txt”
命令=$(cat“${lastExecutionTimeFileName}”
fi
其他的
eval${command}
fi

简而言之,它使用一个文件来记住上次运行的时间。因此,这是一个答案,但我仍然认为它不雅观。

这个问题可能在某种程度上被简化为“一个命令如何在不保存文件的情况下在执行之间记住某些内容?”这个问题可能在某种程度上被简化为“如果不保存文件,命令如何在执行之间记住某些内容?"不幸的是,Motion会自动启动并执行许多无阻塞的执行。这是一个好主意。不幸的是,Motion会自动启动并执行许多无阻塞的执行。这是一个好主意。呃,它看起来如此不雅的部分原因是因为它是如何编码的,而不是前提本身……例如,你应该放弃它de>command=$(cat…)…eval$commandstuff,只需使用shell函数即可。呃,它看起来如此不雅观的部分原因是因为它的编码方式,而不是前提本身……例如,您应该删除
command=$(cat…)…eval$command
stuff,只需使用shell函数即可。