Skip to content

Instantly share code, notes, and snippets.

@tcz
Last active December 25, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcz/6999795 to your computer and use it in GitHub Desktop.
Save tcz/6999795 to your computer and use it in GitHub Desktop.
Script to monitor directory and execute command when changes

Script to monitor directory and execute command when changes

This script monitors a specific directory for changes and when it detects any, it executes a command. It is very useful when writing unit tests for example because you don't need to switch windows. With a 2 monitor setup it's la leche.

Installation

# on Debian/Ubuntu use apt-get
yum -y install inotify-tools wget
wget https://gist.github.com/tcz/6999795/raw/auto.sh
chmod +x auto.sh

Usage

auto.sh DIRECTORY_TO_MONITOR COMMAND_TO_EXECUTE

Example

../auto.sh . phpunit

or

../auto.sh . behat

or

../auto.sh css "lessc css/* > css_compiled/single.css"
#!/bin/sh
if [ $# -ne 2 ]; then
echo "usage: $0 DIRECTORY COMMAND"
exit 64
fi
testsDir=$1
command=$2
if [ ! -d $testsDir ]; then
echo -e "\e[0;31mERROR: invalid dirextory $testsDir\e[0m"
exit 1
fi
while true; do
clear
$command
inotifywait -qre close_write $testsDir
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment