不要在 60 秒内重新启动服务超过 1 次 inotify
Don't restart service more than 1 time within 60 seconds inotify
我有一个文件更改后重启httpd服务的脚本,如何实现速率控制以在60秒内只重启一次服务
我知道该怎么做:
将当前时间与修改 log.txt 时的时间进行比较,但不知道如何开始
#!/bin/bash
mypidfile=/var/run/filewatch.pid
trap "rm -f $mypidfile" EXIT
echo $$ > "$mypidfile"
stdbuf -oL inotifywait -m /home/centos -r -e modify > log.txt |
while read path action file >> log.txt; do
if [[ "$file" =~ .*py$ ]] || [[ "$file" =~ .*css$ ]] || [[ "$file" =~ .*html$ ]] || [[ "$file" =~ .*js$ ]] ; then # Does the file end with .py css html js
systemctl restart httpd # If so, do your thing here!
#touch /home/centos/log.txt
echo "test"
fi
done
- 记得上次使用
lasttime=$(date +%s)
重新启动 httpd
- 如果触发下一次重启,休眠到指定时间减去上次重启与现在的差值
difftime=$(($(date +%s) - lasttime)); if (( difftime < 60 )); then sleep $(( 60 - difftime )); fi; lasttime=$( ... )
很像:
if [[.... ]] ; then # Does the file end with .py css html js
// delay up until 60 seconds from the last restart
if [ -n "${lasttime:-}" ]; then
difftime=$((60 - ($(date +%s) - lasttime)))
if ((difftime > 0)); then
sleep "$difftime"
fi
fi
lasttime=$(date +%s)
systemctl restart httpd # If so, do your thing here!
#touch /home/centos/log.txt
echo "test"
fi
感谢@Kamil Cuk 的建议,我解决了这个问题:
inotifywait -m /home/centos -r -e modify |
while read path action file>>log.txt; do
if [[ "$file" =~ .*py$ ]] || [[ "$file" =~ .*css$ ]] || [[ "$file" =~ .*html$ ]] || [[ "$file" =~ .*js$ ]] ; then # Does the file end with .py css html js
lastreboot=$(cat last_restart.txt)
currenttime=$(date +%s)
let elapsed=currenttime-lastreboot
if [ "$elapsed" -lt 60 ]; then echo "less"
else
echo "restarting HTTPD"
systemctl restart httpd
echo $(date +%s)> last_restart.txt
fi
fi
done
我有一个文件更改后重启httpd服务的脚本,如何实现速率控制以在60秒内只重启一次服务
我知道该怎么做:
将当前时间与修改 log.txt 时的时间进行比较,但不知道如何开始
#!/bin/bash
mypidfile=/var/run/filewatch.pid
trap "rm -f $mypidfile" EXIT
echo $$ > "$mypidfile"
stdbuf -oL inotifywait -m /home/centos -r -e modify > log.txt |
while read path action file >> log.txt; do
if [[ "$file" =~ .*py$ ]] || [[ "$file" =~ .*css$ ]] || [[ "$file" =~ .*html$ ]] || [[ "$file" =~ .*js$ ]] ; then # Does the file end with .py css html js
systemctl restart httpd # If so, do your thing here!
#touch /home/centos/log.txt
echo "test"
fi
done
- 记得上次使用
lasttime=$(date +%s)
重新启动 httpd
- 如果触发下一次重启,休眠到指定时间减去上次重启与现在的差值
difftime=$(($(date +%s) - lasttime)); if (( difftime < 60 )); then sleep $(( 60 - difftime )); fi; lasttime=$( ... )
很像:
if [[.... ]] ; then # Does the file end with .py css html js
// delay up until 60 seconds from the last restart
if [ -n "${lasttime:-}" ]; then
difftime=$((60 - ($(date +%s) - lasttime)))
if ((difftime > 0)); then
sleep "$difftime"
fi
fi
lasttime=$(date +%s)
systemctl restart httpd # If so, do your thing here!
#touch /home/centos/log.txt
echo "test"
fi
感谢@Kamil Cuk 的建议,我解决了这个问题:
inotifywait -m /home/centos -r -e modify |
while read path action file>>log.txt; do
if [[ "$file" =~ .*py$ ]] || [[ "$file" =~ .*css$ ]] || [[ "$file" =~ .*html$ ]] || [[ "$file" =~ .*js$ ]] ; then # Does the file end with .py css html js
lastreboot=$(cat last_restart.txt)
currenttime=$(date +%s)
let elapsed=currenttime-lastreboot
if [ "$elapsed" -lt 60 ]; then echo "less"
else
echo "restarting HTTPD"
systemctl restart httpd
echo $(date +%s)> last_restart.txt
fi
fi
done