inotifywait在bash脚本中没有执行while循环

问题描述 投票:1回答:1

我想在我的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和循环的正确方法是什么?

linux bash sh alpine inotify
1个回答
2
投票

您使用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
© www.soinside.com 2019 - 2024. All rights reserved.