美文网首页
inotify-tools文件监控脚本

inotify-tools文件监控脚本

作者: 张大胆_6a19 | 来源:发表于2019-05-17 16:25 被阅读0次

    一、centos 7安装inotify-tools

    yum install -y epel-release && yum update
    yum --enablerepo=epel install inotify-tools
    

    二、参考网页 https://blog.xjpvictor.info/2017/04/inotify-on-boot-for-systemd/

    a) 给出脚本,命名为inotifywait.sh

    #!/bin/bash
    while /usr/bin/inotifywait -sr -e attrib,modify,create,delete "${1}"
    do
      #执行你要的任何命令,比如重启workerman
      echo extension=event.so >> /var/log/web_watch.log 
    done
    

    b)执行命令,即监控/mnt/hgfs/CentOSClient2Share/workerman-tool/application

    sh inotifywait.sh /mnt/hgfs/CentOSClient2Share/workerman-tool/application
    

    c)根据需要配置开启启动,参考https://www.jianshu.com/p/67103aa278b8
    d)注意点,window更改虚拟机共享文件夹上的文件,不起作用。只能通过ftp方式

    e)实践
    最终得出脚本

    #!/bin/bash
    
    #根据进程名杀死进程
     
    PROCESS=`ps -ef|grep /usr/bin/inotifywait|grep -v grep|grep -v PPID|awk '{ print $2}'`
    for i in $PROCESS
    do
      echo "Kill the inotifywait process [ $i ]"
      kill -9 $i
    done
    
    
    while /usr/bin/inotifywait -o "${1}/inotifywait.log" --format '%T %w%f %e' --timefmt '%y-%m-%d %H:%M:%S' -sr -e attrib,modify,create,delete "${1}/application"
    do
        cd ${1}
        echo -e "\033[35${1}\033[0m"
        /usr/bin/php think worker stop
        echo "stop worker..."
        /usr/bin/php think worker -d
        echo "start worker..."
    
        /usr/bin/php think worker:rpc stop
        echo "stop rpc..."
        /usr/bin/php think worker:rpc -d
        echo "start rpc..."
    done
    

    最终执行命令

    sh /home/app/member/inotifywait.sh /home/app/member
    

    相关文章

      网友评论

          本文标题:inotify-tools文件监控脚本

          本文链接:https://www.haomeiwen.com/subject/kpuoaqtx.html