DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple Files Watch Associated To Commands

08.20.2009
| 3129 views |
  • submit to reddit
        The is the scenario: you want to run a command wherever a file (or files) is modified. 

This is the most basic approach (you will need inotify):

#!/bin/bash
# Usage: onchange.sh "COMMAND" FILE1 ... [FILEN]
set -e
RUNFIRSTLOOP=1

COMMAND=$1
shift
test "$RUNFIRSTLOOP" && bash -c "$COMMAND" || { echo "retcode: $?"; }

while inotifywait "$@"; do
  bash -c "$COMMAND" || { echo "retcode: $?"; }
done