gpt4 book ai didi

linux - inotifywait 不在 bash 脚本中执行 while 循环

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:02:50 25 4
gpt4 key购买 nike

我想在我的 Docker 容器中的目录上放置一个文件观察器。我正在使用 entrypoint.sh 脚本来设置放置文件观察器的脚本。设置如下:

#!/bin/sh

# Trigger the script with the file watcher in the background
./bin/watcher.sh &

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

但是文件永远不会被创建,也没有任何回显。在 bash 脚本中循环使用 inotifywait 的正确方法是什么?

最佳答案

您使用 --outfile 选项明确地将输出发送到文件而不是 stdout。没有任何内容写入stdout,因此while 循环中的read 语句永远不会读取任何数据。

你可能想要:

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

关于linux - inotifywait 不在 bash 脚本中执行 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55813061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com