inotify,独立使用

2022.01.09,更新此脚本
添加了excludePath为数组,可以添加任意多的排除目录,不用每个都做判断,批量判断

#/bin/bash
#配置系统参数
echo 16384 > /proc/sys/fs/inotify/max_queued_events
echo 128 > /proc/sys/fs/inotify/max_user_instances
echo 9999999 > /proc/sys/fs/inotify/max_user_watches

excludePath=(‘/data/web/db/data/tempstatic’ ‘/data/web//data/tempstatic’ ‘/data/web/123/runtime’ ‘/data/web/123_db/runtime’)
#excludePath数组长度为num
num=${#excludePath[@]}
stat=0

inotifywait -mrq –timefmt ‘%Y%m%d%H%M%S’ –format ‘%T %w%f %e’ –event delete,modify,create,attrib,move,moved_to,moved_from /data/web | while read date file event
do
case $event in
MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
if [ “${file##*.}”x = “php”x ]||[ “${file##*.}”x = “js”x ]||[ “${file##*.}”x = “html”x ];then
for filepath in ${excludePath[@]}
do
realFilePath=`dirname $file`
res=`echo $realFilePath|grep $filepath`

if [ -z $res ];then
stat=`expr $stat + 1`
fi
done
#上面for循环,如果找到,则stat一定是3
if [ $stat -eq $num ];then
echo $event’ – ‘$file’ – ‘$date >> /var/log/web_watch.log
stat=0
else
stat=0
fi
fi
;;

MOVED_TO|MOVED_FROM|DELETE|DELETE,ISDIR)
if [ “${file##*.}”x = “php”x ]||[ “${file##*.}”x = “js”x ]||[ “${file##*.}”x = “html”x ];then
for filepath in ${excludePath[@]}
do
realFilePath=`dirname $file`
res=`echo $realFilePath|grep “$filepath”`

if [ -z $res ];then
stat=`expr $stat + 1`
fi
done
#上面for循环,如果找到,则stat一定是3
if [ $stat -eq $num ];then
echo $event’ – ‘$file’ – ‘$date >> /var/log/web_watch.log
stat=0
else
stat=0
fi
fi
;;
esac
done

结束

 

 

 


#/bin/bash

    inotifywait -mrq --timefmt '%Y%m%d%H%M%S' --format '%T %w%f %e' --event delete,modify,create,attrib,move,moved_to,moved_from /data/web | while read date file event
    do
        case $event in
           MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
                if [ "${file##*.}"x = "php"x ]||[ "${file##*.}"x = "js"x ]||[ "${file##*.}"x = "html"x ];then
                    echo $event' - '$file' - '$date >> /var/log/web_watch.log
                fi
                ;;

        MOVED_TO|MOVED_FROM|DELETE|DELETE,ISDIR)
            if [ "${file##*.}"x = "php"x ]||[ "${file##*.}"x = "js"x ]||[ "${file##*.}"x = "html"x ];then
            echo $event' - '$file' - '$date >> /var/log/web_watch.log
            fi
            ;;
       esac
done

 

2022.01.03改写后的shell脚本

#/bin/bash
#配置系统参数
echo 16384 > /proc/sys/fs/inotify/max_queued_events
echo 128 > /proc/sys/fs/inotify/max_user_instances
echo 9999999 > /proc/sys/fs/inotify/max_user_watches
#排除bbb和aaa目录的文件变动

excludePath="/data/web/bbb/data/tempstatic"
excludePath2="/data/web/aaa/data/tempstatic"

inotifywait -mrq --timefmt '%Y%m%d%H%M%S' --format '%T %w%f %e' --event delete,modify,create,attrib,move,moved_to,moved_from /data/web | while read date file event
do
    case $event in
       MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
           if [ "${file##*.}"x = "php"x ]||[ "${file##*.}"x = "js"x ]||[ "${file##*.}"x = "html"x ];then
               realFilePath=`dirname $file`
               res=`echo $realFilePath|grep $excludePath`
               res2=`echo $realFilePath|grep $excludePath2`

           if [ -z $res ] && [ -z $res2 ];then
                echo $event' - '$file' - '$date >> /var/log/web_watch.log
            fi

         fi
      ;;

       MOVED_TO|MOVED_FROM|DELETE|DELETE,ISDIR)
            if [ "${file##*.}"x = "php"x ]||[ "${file##*.}"x = "js"x ]||[ "${file##*.}"x = "html"x ];then
                  realFilePath=`dirname $file`
                   res=`echo $realFilePath|grep "$excludePath"`
                   res2=`echo $realFilePath|grep $excludePath2`

             if [ -z $res ] && [ -z $res2 ];then
                  echo $event' - '$file' - '$date >> /var/log/web_watch.log
                  #echo $event' - '$file' - '$date >> /data/web/zx123/analyseLogs/monitorChangeLog/web_watch.log
             fi
         fi
       ;;
       esac
done

 

通过使用nohup /home/sh/inotify_web.sh &
让程序在后台运行

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注