#!/bin/bash
#ver 1.2 by gavin
#need inotify-tools.x86_64 && inotify-tools-devel.x86_64 package
#yum -y install inotify-tools.x86_64 inotify-tools-devel.x86_64
usage_info () {
echo "Usage: $0 optstring parameters"
echo " --verbose increase verbosity info"
}
install_package () {
echo "need inotify-tools.x86_64 && inotify-tools-devel.x86_64 package"
echo ""
echo "rpm --import "
echo "rpm -Uvh
.i386.rpm"
echo "rpm -Uvh .
rf.x86_64.rpm"
echo "yum -y install inotify-tools.x86_64 inotify-tools-devel.x86_64"
echo ""
}
SOR_DIR=/targeting/data
EVENTS=(-e CREATE,MODIFY,MOVED_TO,DELETE)
TARGET_FILE=word_targeting_mobile.db
PID=/tmp/monitor_file.pid
monitor_file () {
while inotifywait ${EVENTS[@]} --exclude word_targeting_mobile.db $SOR_DIR; do
ls -rt $SOR_DIR |grep -v $TARGET_FILE|tail -n1
ino_file=$SOR_DIR/`ls -rt $SOR_DIR|grep -v $TARGET_FILE |tail -n1`
echo $ino_file
echo "get a new file"
rm -rf $SOR_DIR/$TARGET_FILE
ln -s $ino_file $SOR_DIR/$TARGET_FILE
echo "ln -s $ino_file -> $SOR_DIR/$TARGET_FILE"
done
}
main () {
[ ! -x /usr/bin/inotifywait ] && install_package
[ -e $PID ] && ps -ef |awk '{print $2}' |grep `cat $PID`|grep -v grep &>/dev/null
if [ `echo $?` -eq 0 ]
then
echo "$0 can not run : process is already running in `cat $PID` pid"
exit 1
fi
if [ $# -eq 0 ]
then
monitor_file &>/dev/null &
echo $! > $PID
elif [ "$1" == "--verbose" ]
then
monitor_file
else
usage_info
exit 1
fi
}
main $@
exit 0
阅读(1229) | 评论(0) | 转发(0) |