Last Updated: February 25, 2016
·
1.604K
· Tomás Prado

Phing: watch over files changes

You could make a script that generates a md5 hash over a files folder and then for each 10 seconds check for changes.

I made a phing task that calls a script md5Watch.sh

<target name="watch" description="Watch changes in a folder">
    <echo msg="Starting (Close with Ctrl+C)..." />
    <exec command="touch ~/md5Watch.txt"/>
    <exec command="chmod 777 ~/md5Watch.txt"/>
    <exec command="echo 1 > ~/md5Watch.txt"/>

    <exec command="./md5Watch.sh" passthru="true" checkreturn="true" />
</target>

And then the script (md5Watch.sh):

#!/bin/sh
echo "\tBegin watching...";

while true
do
    if [ $? -eq 0 ]
    then
        OLDVALUE=`cat ~/md5Watch.txt`
        NEWVALUE=`find YOUR/FILES/FOLDER/PATH -type f -exec md5sum '{}' + | sort | md5sum`

        if [ ${OLDVALUE} != ${NEWVALUE} ]
        then
            date=`date "+%H:%M:%S"`
            echo "$date Files changed";

            //EXECUTE HERE WHATEVER YOU WANT TO EXECUTE WHEN THE FILES CHANGE

            echo ${NEWVALUE} > ~/md5Watch.txt
            echo "";
        fi

    fi

    sleep 10
done

You have to only change the YOUR/FILES/FOLDER/PATH to the folder path where your files are.

Then, when the md5 changes (that is when if [ ${OLDVALUE} != ${NEWVALUE} ] is true), execute whatever you want to execute.

2 Responses
Add your response

Although is not related to Phing, you may be intrested in an alternative to watch when a file or directory changed: https://coderwall.com/p/i8tt4w

over 1 year ago ·

its very simple to use and install
thanks for sharing!

over 1 year ago ·