嗯,这是我每次检测到动作时运行的脚本类型:
#!/bin/bash
#user variables #userSet
timeIntervalInSecondsBetweenCommandExecutions=120
lastExecutionTimeFileName="last_command_execution_time.txt"
command=$(cat << 2012-08-20T1654
twidge update "@ 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
简而言之,它正在使用一个文件记住上次运行的时间。所以,这是一个答案,但我仍然认为它不够优雅。