Skip to content

Instantly share code, notes, and snippets.

@gchristofferson
Last active December 21, 2018 00:47
Show Gist options
  • Save gchristofferson/9d090fb93a78878b083ee1681a189ac2 to your computer and use it in GitHub Desktop.
Save gchristofferson/9d090fb93a78878b083ee1681a189ac2 to your computer and use it in GitHub Desktop.
This script takes one command line argument [directory]. It watches the directory for any changes and executes git commands on change.
#!/bin/sh
# the script takes one command line argument which is the name of the directory to watch for changes
check() {
dir="$1"
chsum1=`find $dir -type f -mtime -5 -exec md5sum {} \;`
chsum2=$chsum1
changed=false
pushed=false
declare -i waited
waited=0
while [[ $chsum1 == $chsum2 || $chsum1 != $chsum2 ]]
do
if [[ $chsum1 == $chsum2 ]]
then
echo "directory hasn't changed"
sleep 10
if [[ $pushed == true ]]
then
changed=false
pushed=false
fi
if [[ $changed == true ]]
then
waited=$waited+10
echo "waited $waited seconds"
fi
if [ $waited -gt 50 ] && [ $changed == true ];
then
cd $dir
echo "pushing changes to git"
git status
git add .
git commit -m "directly modified files on live server"
git push origin master
echo "backing up database"
cd /home/dh_mcbjqv/db_backup
/home/dh_mcbjqv/db_backup/db_backup.sh
changed=false
pushed=true
waited=0
cd ..
fi
chsum2=`find $dir/ -type f -mtime -5 -exec md5sum {} \;`
else
echo "something has changed"
changed=true
sleep 10
waited=0
echo "waited $waited secconds"
chsum1=`find $dir/ -type f -mtime -5 -exec md5sum {} \;`
chsum2=$chsum1
fi
done
}
check $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment