Inotify File Watcher Action Script Prompt
Generate a reliable inotifywait-based watcher that triggers an action on file/directory changes, with event debouncing, recursive watching, atomic-write handling, and a systemd unit so it survives reboots.
- Target user
- Engineers automating reload/sync/build on filesystem changes
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who has built file watchers that don't fire 200 times for one save and don't silently die after a logrotate. I will provide: - The directory/path to watch and whether it's recursive - The action to run on change (reload, sync, rebuild, notify) - The editors/tools writing the files (matters for atomic saves) Your job: 1. **Tool choice** — use `inotifywait` (inotify-tools). State the dependency and the Linux-only caveat (offer `fswatch` as the macOS alternative). Avoid naive polling loops unless inotify is unavailable. 2. **Event selection** — watch the RIGHT events. Many editors write via temp-file + rename, so watch `close_write,moved_to,create` rather than every `modify`. Explain why watching raw `modify` causes storms. Use `-m` (monitor mode), `-r` for recursive, `--format` for parseable output, and `-e` to scope events. 3. **Debounce / coalesce** — a single save can emit several events. Implement debouncing: collect events for a short window (e.g. 500ms) and run the action once. Show a timestamp-based debounce in pure bash (background timer / last-event marker) without busy-waiting. 4. **Atomic action execution** — guard against overlapping runs: use a lockfile (`flock`) so a long action doesn't get re-entered; if a change arrives mid-run, mark "dirty" and re-run once afterward. 5. **Filtering** — include/exclude patterns (`--exclude` regex) to ignore `.git/`, `node_modules/`, swap files, and the watcher's own output. Prevent feedback loops where the action modifies a watched file. 6. **Resilience** — handle the watched dir being deleted/recreated (re-establish the watch), log each trigger with a timestamp, and exit non-zero on fatal inotify errors. Note the `max_user_watches` sysctl and how to raise it for large trees. 7. **Run as a service** — provide a systemd unit (Restart=on-failure, proper User=, WorkingDirectory=) so it auto-restarts and starts on boot; mention journald logging. Output: (a) the complete watcher script, (b) the systemd unit file, (c) the event/format flags chosen with rationale, (d) a note on raising max_user_watches, (e) how to test it (touch/rename and observe one trigger). Bias toward: one action per logical change, no feedback loops, and surviving editor temp-file behavior.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Bash Health-Gated Service Restart Orchestrator Prompt
Build a Bash script that restarts services one at a time, waits for each to pass a health check before moving on, and rolls back or aborts on failure to avoid taking a whole fleet down at once
-
Bash Cron-to-systemd-Timer Migration Prompt
Convert legacy crontab entries into equivalent systemd service+timer units with correct scheduling, logging, environment, and failure handling so scheduled jobs become observable and manageable.
-
Bash Multi-Repo Git Bulk Command Runner Prompt
Build a Bash script that runs the same git or shell command across many local repositories with per-repo isolation, dirty-tree guards, and an aggregated pass/fail summary
-
Bash Safe In-Place sed Edit Wrapper Prompt
Build a Bash wrapper around sed -i that backs up, validates, and atomically applies in-place edits so a bad expression can never corrupt or truncate config files
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.