Archive

Archive for August, 2009

Detect directory or file changes in *nix

August 14th, 2009

There are various wasys to do this, but this is what I implemented. It is working as expected as of now on my RHEL 5.x boxes. I’ll take my usecase here and describe things.

Plesk web hosting control panel is managing several hundred domains on one of RHEL box. Addition and removal of domains is very common. We need to sync all domain’s httpdocs directory to other webservers. Of course, a simple rsync can be setup for this but it’s much efficient that rsync should only run when there’s any change, ie addition/removal of a domain or file updation etc. What I mean to say, instead of letting rsync detect changes, its better that our script should detect changes and then run rsync. The obvious advantage is that network burden is reduced because rsync will only sync contents with our servers when there are changes.

The domains are stored in their individual directories in /var/www/vhosts path. Therefore, vhosts is the directory we are watching, if we detect any change in it, we’ll store names of all of its subdirectories in a text file and then can call rsync with domain’s httpdocs as argument to sync.

Here is the script:

#!/bin/bash

############### detectdir.sh by Jagbir Singh #################
#
# script to detect changes in directory.
#
###############################################################

# directory to watch
DIR=”/var/www/vhosts”

# store current statistics of dir
OLD=`stat -t $DIR`

while true
do

# take a new snapshot of stats
NEW=`stat -t $DIR`

# compare it with old
if [ "$NEW" != "$OLD" ]; then

echo “changed!” ## you may want to comment this

# take current listing of dir in a file. domains may be added or removed.
ls $DIR –file-type | grep “\/” | sed ’s/\///’ > /tmp/dir.list

# open file and you can now process entries in it
exec 10 let count=0

while read LINE <&10; do

# currently printed on screen, can supply this as arg to rSync, discussed later
echo $LINE/httpdocs/
echo
((count++))
done

# take snapshot again and store it in both old and new vars
NEW=`stat -t $DIR`
OLD=$NEW
exec 10>&-
fi

# i’m using 3 secs calm time, you should update this as per your environment
sleep 3
done

I’ve skipped rsync command here because that is already discussed in detail in other post here in which separate script is there to execute rsync on demand.

General, Web Server, bash

Get Adobe Flash playerPlugin by wpburn.com wordpress themes