Skip to content

Instantly share code, notes, and snippets.

@hexode
Forked from wernight/inotifyexec.py
Created April 29, 2014 15:24
Show Gist options
  • Select an option

  • Save hexode/11403574 to your computer and use it in GitHub Desktop.

Select an option

Save hexode/11403574 to your computer and use it in GitHub Desktop.

Revisions

  1. @wernight wernight revised this gist Apr 29, 2014. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions inotifyexec
    Original file line number Diff line number Diff line change
    @@ -27,17 +27,16 @@ lock_tmp_file=$(mktemp)

    # Listen to a single file changes, should be the temporary file changes.
    inotify_single_file() {
    # Ignore errors when executing the command.
    set +e

    while inotifywait -qq -e attrib $trigger_tmp_file
    do
    # Create the lock after all touch even have passed.
    sleep 0.5
    touch $lock_tmp_file

    # Execute the desired command.
    set +e
    $exec_command
    set -e

    # Delete the lock
    rm -rf $lock_tmp_file
  2. @wernight wernight revised this gist Apr 29, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion inotifyexec
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # Help?
    if [[ $# -eq 0 || $1 == --help || $1 == -h ]]
    then
    echo "inotifyexec version 1.1"
    echo "inotifyexec version 1.1.0"
    echo "Requires inotify-tools."
    echo ""
    echo "Usage: inotifyexec <command> <inotifywait arguments...>"
  3. @wernight wernight revised this gist Apr 29, 2014. 1 changed file with 28 additions and 9 deletions.
    37 changes: 28 additions & 9 deletions inotifyexec
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,11 @@
    # Help?
    if [[ $# -eq 0 || $1 == --help || $1 == -h ]]
    then
    echo "inotifyexec version 1.0"
    echo "inotifyexec version 1.1"
    echo "Requires inotify-tools."
    echo ""
    echo "Usage: inotifyexec <command> <inotifywait arguments...>"
    echo "Example: inotifyexec make -r . -qq -e modify,create,delete --exclude '*.swp'"
    echo "Example: inotifyexec make -r . -e modify,create,delete --exclude '*.swp'"
    echo ""
    echo "Once watch established, press Ctrl + C once to shutdown the watch cleanly."
    if [[ $# -eq 0 ]]
    @@ -22,29 +22,48 @@ fi

    exec_command=$1
    shift
    inotifyargs=$*
    tmp_file=$(mktemp)
    trigger_tmp_file=$(mktemp)
    lock_tmp_file=$(mktemp)

    # Listen to a single file changes, should be the temporary file changes.
    inotify_single_file() {
    # Ignore errors when executing the command.
    set +e

    while inotifywait -qq -e attrib $tmp_file
    while inotifywait -qq -e attrib $trigger_tmp_file
    do
    # Create the lock after all touch even have passed.
    sleep 0.5
    touch $lock_tmp_file

    # Execute the desired command.
    $exec_command

    # Delete the lock
    rm -rf $lock_tmp_file
    done
    }

    # Fork a listener on the temporary file.
    touch $tmp_file
    inotify_single_file &
    rm -rf $lock_tmp_file

    # Keep listening to the desired directory:
    # Each change will touch the temporary file again.
    inotifywait --monitor $inotifyargs | while read dir ev file
    inotifywait --monitor $* | while read dir ev file
    do
    touch $tmp_file
    # If there is a lock wait until it's unlocked
    if [[ -e $lock_tmp_file ]]
    then
    inotifywait -qq -e delete $lock_tmp_file || true

    # Wait until watch has been established again
    sleep 0.2
    fi

    # Tell a change happened.
    touch $trigger_tmp_file
    done

    rm -f $tmp_file
    rm -f $trigger_tmp_file 2> /dev/null
    rm -f $lock_tmp_file 2> /dev/null
  4. @wernight wernight revised this gist Apr 29, 2014. 1 changed file with 14 additions and 21 deletions.
    35 changes: 14 additions & 21 deletions inotifyexec
    Original file line number Diff line number Diff line change
    @@ -23,35 +23,28 @@ fi
    exec_command=$1
    shift
    inotifyargs=$*
    tmp_file=$(mktemp)

    # Listen to a single file changes, should be the temporary file changes.
    if [[ "$#" -eq 1 && -e $1 ]]
    then
    inotify_single_file() {
    # Ignore errors when executing the command.
    set +e

    while inotifywait -qq -e attrib $1
    while inotifywait -qq -e attrib $tmp_file
    do
    $exec_command
    done
    else
    tmp_file=$(mktemp)
    touch $tmp_file
    }

    # Fork a listener on the temporary file.
    $0 "$exec_command" $tmp_file &
    # Fork a listener on the temporary file.
    touch $tmp_file
    inotify_single_file &

    # Keep listening to the desired directory:
    # Each change will touch the temporary file again.
    inotifywait --monitor $inotifyargs | while read dir ev file
    do
    if [[ "$exec_command" == "" ]]
    then
    echo "dir:$dir ev:$ev file:$file"
    else
    touch $tmp_file
    fi
    done
    # Keep listening to the desired directory:
    # Each change will touch the temporary file again.
    inotifywait --monitor $inotifyargs | while read dir ev file
    do
    touch $tmp_file
    done

    rm -f $tmp_file
    fi
    rm -f $tmp_file
  5. @wernight wernight created this gist Apr 29, 2014.
    57 changes: 57 additions & 0 deletions inotifyexec
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    #!/bin/bash -e
    #
    # Author: Werner Beroux <werner@beroux.com>

    # Help?
    if [[ $# -eq 0 || $1 == --help || $1 == -h ]]
    then
    echo "inotifyexec version 1.0"
    echo "Requires inotify-tools."
    echo ""
    echo "Usage: inotifyexec <command> <inotifywait arguments...>"
    echo "Example: inotifyexec make -r . -qq -e modify,create,delete --exclude '*.swp'"
    echo ""
    echo "Once watch established, press Ctrl + C once to shutdown the watch cleanly."
    if [[ $# -eq 0 ]]
    then
    exit 1
    else
    exit 0
    fi
    fi

    exec_command=$1
    shift
    inotifyargs=$*

    # Listen to a single file changes, should be the temporary file changes.
    if [[ "$#" -eq 1 && -e $1 ]]
    then
    # Ignore errors when executing the command.
    set +e

    while inotifywait -qq -e attrib $1
    do
    $exec_command
    done
    else
    tmp_file=$(mktemp)
    touch $tmp_file

    # Fork a listener on the temporary file.
    $0 "$exec_command" $tmp_file &

    # Keep listening to the desired directory:
    # Each change will touch the temporary file again.
    inotifywait --monitor $inotifyargs | while read dir ev file
    do
    if [[ "$exec_command" == "" ]]
    then
    echo "dir:$dir ev:$ev file:$file"
    else
    touch $tmp_file
    fi
    done

    rm -f $tmp_file
    fi