inotifywait不执行bash脚本中的while循环 [英] inotifywait not performing the while loop in bash script

查看:162
本文介绍了inotifywait不执行bash脚本中的while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件监视程序放在Docker容器的目录中.我正在使用 entrypoint.sh 脚本来设置放置文件监视程序的脚本.设置如下:

I want to put a file watcher on a directory in my Docker container. I'm using entrypoint.sh script to setup the script that places a file watcher. The setup is like so:

#!/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 检查时列出了观察者,并且它报告了已定义的日志文件中的更改,但是循环永远不会触发.我试过用简单的方法调试循环:

However, although the watcher is listed when i check with top, and it reports changes in the defined log file, the loop never triggers. I've tried debugging the loop with simple:

    touch /var/log/special.log
    echo "${CHANGED}" >> /var/log/special.log

但是永远不会创建文件,也不会回显任何内容.在bash脚本中将 inotifywait 与循​​环一起使用的正确方法是什么?

But file never gets created, and nothing gets echoed in it. What is the right way to use inotifywait with loop in bash script?

推荐答案

您正在使用-outfile 选项将输出显式发送到文件,而不是 stdout .什么都不会写入到 stdout ,因此在 while 循环中的 read 语句永远不会读取任何数据.

You are explicitly sending output to a file rather than stdout using the --outfile option. Nothing is ever written to stdout, so the read statement in your while loop never reads any data.

您可能想要:

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

这篇关于inotifywait不执行bash脚本中的while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆