我想在 Docker 容器中的目录上放置一个文件观察器。我在用着entrypoint.sh
脚本来设置放置文件观察器的脚本。设置如下:
#!/bin/sh
# Trigger the script with the file watcher in the background
./bin/watcher.sh &
And the watcher.sh
脚本包含inotifywait
命令:
#!/bin/sh
inotifywait \
--event create --event delete \
--event modify --event move \
--format "%e %w%f" \
--monitor --outfile '/var/log/inotifywait.log' \
--syslog --quiet --recursive \
/etc/haproxy |
while read CHANGED;
do
echo "$CHANGED"
haproxy -W -db -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid) &
done
然而,尽管当我检查时观察者已列出top
,并且它报告定义的日志文件中的更改,循环永远不会触发。我尝试用简单的方法调试循环:
touch /var/log/special.log
echo "${CHANGED}" >> /var/log/special.log
但文件永远不会被创建,并且没有任何内容在其中得到回显。正确的使用方法是什么inotifywait
在 bash 脚本中使用循环?
您明确地将输出发送到文件而不是stdout
使用--outfile
选项。没有任何内容被写入stdout
, 所以read
声明在你的while
循环从不读取任何数据。
您可能想要:
inotifywait \
--event create --event delete \
--event modify --event move \
--format "%e %w%f" \
--monitor \
--syslog --quiet --recursive \
/etc/haproxy |
while read CHANGED;
do
echo "$CHANGED"
haproxy -W -db -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid) &
done
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)