CodexBloom - Programming Q&A Platform

Bash script scenarios to detect changes in a watched directory with inotifywait on RHEL 8

👀 Views: 73 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
bash linux inotify

I'm trying to use a Bash script to monitor changes in a specific directory using `inotifywait`. My goal is to trigger a specific action whenever a file is added or modified. However, the script seems to run indefinitely but does not detect any changes. Here's the code I'm using: ```bash #!/bin/bash WATCH_DIR="/path/to/watch" # Run inotifywait to watch for create and modify events inotifywait -m -r -e create -e modify "$WATCH_DIR" | while read path action file; do echo "The file '$file' was just $action in directory '$path'" # Your action here done ``` I've made sure that the path is correct and that the user running the script has appropriate permissions to the directory. I also installed `inotify-tools` using `yum install inotify-tools`. When I create or modify a file in the watched directory, I expect the script to output the action, but it remains silent. I've tried running the script with both `bash script.sh` and `./script.sh`, and I also checked the version of `inotifywait` using `inotifywait --version`, which returns `inotify-tools 3.20.11`. One thing I noticed is that if I manually run `inotifywait` in the terminal, it seems to detect changes as expected. However, when I run it as part of the script, it doesn't respond. Is there something I'm missing in my script, or is there a specific configuration for RHEL 8 that could affect `inotifywait`? Any insights would be appreciated!