我有一个脚本作为监听文件的守护进程运行:
while inotifywait -e close_write /home/homeassistant/.homeassistant/groups.yaml
do
echo 'gropus'
curl -X POST -H "x-ha-access: pass" -H "Content-Type: application/json" http://hassbian.local:8123/api/services/group/reload
done;
while inotifywait -e close_write /home/homeassistant/.homeassistant/core.yaml
do
echo 'core'
curl -X POST -H "x-ha-access: pass" -H "Content-Type: application/json" http://hassbian.local:8123/api/services/homeassistant/reload_core_config
done;
我想收听几个文件,并尝试再添加两个循环:
{{1}}
我意识到第一个循环永远不会被关闭,所以其他循环永远不会开始,但不知道我应该如何解决这个问题。
答案 0 :(得分:3)
您需要在后台进程中运行第一个循环,以便它不会阻止您的脚本。您可能希望在后台运行每个循环以获得对称性,然后在脚本结束时等待它们。
while inotifywait -e close_write /home/homeassistant/.homeassistant/groups.yaml
do
echo 'gropus'
curl -X POST -H "x-ha-access: pass" -H "Content-Type: application/json" http://hassbian.local:8123/api/services/group/reload
done &
while inotifywait -e close_write /home/homeassistant/.homeassistant/core.yaml
do
echo 'core'
curl -X POST -H "x-ha-access: pass" -H "Content-Type: application/json" http://hassbian.local:8123/api/services/homeassistant/reload_core_config
done &
wait
但是,您可以在监控模式下运行inotifywait
并监控多个文件,将其输出组合成一个循环。 (警告:与任何面向行的输出格式一样,这不能处理包含换行符的文件名。请参阅--format
和--csv
选项以处理包含空格的文件名。)
files=(
/home/homeassistant/.homeassistant/groups.yaml
/home/homeassistant/.homeassistant/core.yaml
)
take_action () {
echo "$1"
curl -X POST "x-ha-access: pass" -H "Content-Type: application/json" \
http://hassbian.local:8123/api/services/"$2"
}
inotifywait -m -e close_write "${files[@]}" |
while IFS= read -r fname _; do
case $fname in
*/groups.yaml) take_action "groups" "group/reload" ;;
*/core.yaml) take_action "core" "homeassistant/reload_core_config" ;;
sac
done