🖌️
Linux命令大全
  • Linux 命令大全
  • 命令
    • vgchange
    • ack
    • login
    • help
    • unset
    • pstree
    • emacs
    • route
    • fg
    • uupick
    • uniq
    • exportfs
    • jed
    • cd
    • bc
    • ab
    • gunzip
    • halt
    • readelf
    • fold
    • ss
    • nologin
    • users
    • unzip
    • head
    • xinit
    • dd
    • chkconfig
    • apropos
    • spell
    • rpmverify
    • ssh
    • seinfo
    • tcpreplay
    • dpkg-preconfigure
    • mkinitrd
    • patch
    • lilo
    • xclip
    • rmmod
    • nm
    • lprm
    • look
    • reject
    • ifdown
    • ifcfg
    • gcc
    • yes
    • mysqlimport
    • ed
    • nslookup
    • ispell
    • nisdomainname
    • rsh
    • ifconfig
    • inotifywait
    • whoami
    • rpmbuild
    • e2label
    • wget
    • sendmail
    • crontab
    • setfacl
    • nl
    • rename
    • jwhois
    • sort
    • lsof
    • passwd
    • exit
    • lvresize
    • cmp
    • smbpasswd
    • tree
    • elm
    • tr
    • kill
    • times
    • pgrep
    • axel
    • gdb
    • fc
    • lha
    • pfctl
    • mknod
    • bind
    • bzdiff
    • nice
    • dpkg-statoverride
    • systool
    • gpasswd
    • tar
    • slocate
    • dpkg-deb
    • file
    • zcat
    • ngrep
    • gpm
    • arptables
    • sshd
    • ipcalc
    • sar
    • basename
    • tmux
    • ftpwho
    • uuto
    • false
    • builtin
    • cksum
    • pwconv
    • rexec
    • rev
    • rm
    • source
    • fdisk
    • sync
    • killall
    • lsb_release
    • id
    • tput
    • repquota
    • uncompress
    • cut
    • ldconfig
    • mysql
    • pidof
    • sesearch
    • shift
    • openssl
    • ftp
    • vgconvert
    • nohup
    • dig
    • iostat
    • strings
    • set
    • type
    • expand
    • cancel
    • chsh
    • gzip
    • umask
    • hping3
    • logsave
    • ltrace
    • pathchk
    • nmcli
    • lscpu
    • zipsplit
    • printf
    • rpmquery
    • zip
    • diffstat
    • runlevel
    • sftp-server
    • find
    • less
    • chown
    • col
    • ifup
    • uname
    • named-checkzone
    • mke2fs
    • pssh
    • disown
    • lftpget
    • scp
    • mii-tool
    • pstack
    • lsattr
    • joe
    • ld
    • chgrp
    • dris
    • xhost
    • xauth
    • userdel
    • unarj
    • ssh-agent
    • lpstat
    • cupsenable
    • grub
    • lynx
    • ipcs
    • write
    • logrotate
    • csplit
    • seq
    • rcp
    • touch
    • lpc
    • quotaoff
    • mail
    • mpstat
    • vgreduce
    • hwclock
    • arp
    • rlogin
    • break
    • mysqlshow
    • syslog
    • gzexe
    • semanage
    • ip6tables-save
    • startx
    • pvchange
    • rpm2cpio
    • chattr
    • grep
    • volname
    • who
    • last
    • lnstat
    • read
    • atq
    • lftp
    • setsebool
    • ssh-keygen
    • bzcat
    • iptables-save
    • jobs
    • arch
    • shutdown
    • shopt
    • sh
    • more
    • iconv
    • chcon
    • od
    • nc
    • lsusb
    • squidclient
    • pwd
    • dircolors
    • which
    • fsck
    • expr
    • partprobe
    • wait
    • dpkg-split
    • get_module
    • chmod
    • vgremove
    • rcconf
    • export
    • nano
    • stat
    • blkid
    • pvdisplay
    • dump
    • elinks
    • atop
    • restorecon
    • showmount
    • suspend
    • apk
    • telint
    • supervisord
    • groups
    • ssh-add
    • fgrep
    • setpci
    • dnf
    • pwunconv
    • swapoff
    • resize
    • kexec
    • insmod
    • install
    • cpio
    • hostname
    • kernelversion
    • ex
    • at
    • unlink
    • let
    • dpkg-divert
    • egrep
    • unexpand
    • popd
    • lsmod
    • slabtop
    • dpkg-trigger
    • vi
    • colrm
    • ftptop
    • depmod
    • cdrecord
    • swapon
    • mailq
    • ping
    • mktemp
    • cu
    • as
    • uucico
    • test
    • man
    • losetup
    • strace
    • dhcrelay
    • tload
    • batch
    • split
    • parted
    • chage
    • dos2unix
    • lsblk
    • locate
    • du
    • bzip2recover
    • arj
    • ar
    • bzless
    • tty
    • exec
    • mount
    • ifstat
    • mkswap
    • grpconv
    • edquota
    • lvreduce
    • tempfile
    • init
    • fishshell
    • dstat
    • logger
    • php
    • tail
    • return
    • htdigest
    • arpwatch
    • dnsdomainname
    • iftop
    • ntpdate
    • renice
    • fping
    • quotacheck
    • quota
    • lvdisplay
    • mapfile
    • dirname
    • ntsysv
    • pwck
    • unalias
    • usermod
    • sftp
    • apt-key
    • dhclient
    • ipcrm
    • quotaon
    • vgrename
    • perl
    • sleep
    • w
    • domainname
    • vdfuse
    • ssh-keyscan
    • dpkg-reconfigure
    • info
    • mtools
    • lvremove
    • tftp
    • zfore
    • modprobe
    • vgscan
    • date
    • mysqldump
    • sysctl
    • cat
    • tac
    • bzgrep
    • nmap
    • tailf
    • bmodinfo
    • useradd
    • whatis
    • make
    • atrm
    • true
    • ftpshut
    • pvck
    • top
    • mkbootdisk
    • chfn
    • htop
    • service
    • trap
    • md5sum
    • ftpcount
    • firewall-cmd
    • ip6tables-restore
    • time
    • bzmore
    • sudo
    • tcpdump
    • iptables-restore
    • awk
    • apt-sortpkgs
    • cp
    • xargs
    • mysqladmin
    • vgcreate
    • wc
    • indent
    • screen
    • groupdel
    • comm
    • tee
    • pmap
    • free
    • curl
    • mkisofs
    • mesg
    • history
    • ip6tables
    • getenforce
    • pigz
    • talk
    • protoize
    • iptables
    • setsid
    • clockdiff
    • vgextend
    • mkfs
    • paste
    • mailstat
    • dhcpd
    • cupsdisable
    • logwatch
    • smbclient
    • bzip2
    • vmstat
    • git
    • iperf
    • wall
    • ppp-off
    • squid
    • join
    • getsebool
    • xset
    • htpasswd
    • logout
    • rmdir
    • grpunconv
    • poweroff
    • pvremove
    • iptraf
    • sed
    • mv
    • finger
    • netstat
    • skill
    • xlsclients
    • whereis
    • fuser
    • eject
    • consoletype
    • usernetctl
    • yum
    • ethtool
    • vgdisplay
    • newusers
    • blockdev
    • alias
    • hdparm
    • host
    • nethogs
    • dmesg
    • accept
    • dpkg
    • uptime
    • lvscan
    • lspci
    • chroot
    • updatedb
    • rpm
    • watch
    • logname
    • hexdump
    • zipinfo
    • nfsstat
    • continue
    • ls
    • iotop
    • ip
    • mkdir
    • aptitude
    • dirs
    • lvcreate
    • dmidecode
    • diff3
    • apt-get
    • stty
    • clock
    • pkill
    • ypdomainname
    • restore
    • ulimit
    • findfs
    • pv
    • znew
    • cal
    • ssh-copy-id
    • lpr
    • bzcmp
    • lastlog
    • bye
    • bg
    • unprotoize
    • convertquota
    • pico
    • diff
    • grpck
    • apachectl
    • env
    • fmt
    • ncftp
    • xlsfonts
    • rpmdb
    • reboot
    • lpq
    • clear
    • echo
    • pvs
    • rsync
    • arpd
    • ldd
    • lastb
    • umount
    • systemctl
    • lvextend
    • bunzip2
    • speedtest-cli
    • iptstate
    • lp
    • traceroute
    • rpmsign
    • pvscan
    • enable
    • objdump
    • command
    • arping
    • badblocks
    • gcov
    • hostid
    • local
    • ps
    • xlsatoms
    • dpkg-query
    • groupmod
    • lpadmin
    • sum
    • chpasswd
    • xz
    • groupadd
    • su
    • pr
    • e2fsck
    • unrar
    • telnet
    • ln
    • declare
    • df
    • compress
    • tracepath
    • pvcreate
    • pushd
    • readonly
Powered by GitBook
On this page
  • 补充说明
  • 安装inotify-tools
  • inotify相关参数
  • inotifywait命令使用
  • inotifywait命令参数
  • 可监听的事件

Was this helpful?

  1. 命令

inotifywait

PreviousifconfigNextwhoami

Last updated 5 years ago

Was this helpful?

异步文件系统监控机制

补充说明

Inotify 一种强大的、细粒度的、异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性、读写属性、权限属性、删除创建、移动等操作,也就是可以监控文件发生的一切变化。。

inotify-tools 是一个C库和一组命令行的工作提供Linux下inotify的简单接口。inotify-tools安装后会得到inotifywait和inotifywatch这两条命令:

  • inotifywait命令 可以用来收集有关文件访问信息,Linux发行版一般没有包括这个命令,需要安装inotify-tools,这个命令还需要将inotify支持编译入Linux内核,好在大多数Linux发行版都在内核中启用了inotify。

  • inotifywatch命令 用于收集关于被监视的文件系统的统计数据,包括每个 inotify 事件发生多少次。

开始之前需要检测系统内核是否支持inotify:

使用uname -r命令检查Linux内核,如果低于2.6.13,就需要重新编译内核加入inotify的支持。

使用ll /proc/sys/fs/inotify命令,是否有以下三条信息输出,如果没有表示不支持。

ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Jan  4 15:41 max_queued_events
-rw-r--r-- 1 root root 0 Jan  4 15:41 max_user_instances
-rw-r--r-- 1 root root 0 Jan  4 15:41 max_user_watches

安装inotify-tools

  • inotify-tools项目地址:

  • inotify-tools下载地址:

#CentOS release 5.8/64位:
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install

inotify相关参数

inotify定义了下列的接口参数,可以用来限制inotify消耗kernel memory的大小。由于这些参数都是内存参数,因此,可以根据应用需求,实时的调节其大小:

  • /proc/sys/fs/inotify/max_queued_evnets表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。

  • /proc/sys/fs/inotify/max_user_instances表示每一个real user id可创建的inotify instatnces的数量上限。

  • /proc/sys/fs/inotify/max_user_watches表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。

根据以上在32位或者64位系统都可以执行:

echo 104857600 > /proc/sys/fs/inotify/max_user_watches
echo 'echo 104857600 > /proc/sys/fs/inotify/max_user_watches' >> /etc/rc.local

如果遇到以下错误:

inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory
 **解决方法:** 
32位系统:ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib/libinotifytools.so.0
64位系统:ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0

inotifywait命令使用

#!/bin/bash
#filename watchdir.sh
path=$1
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e modify,delete,create,attrib $path

执行输出:
./watchdir.sh /data/wsdata/tools/
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swx
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swx
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
04/01/13/16:35 /data/wsdata/tools/ 4913
04/01/13/16:35 /data/wsdata/tools/ 4913
04/01/13/16:35 /data/wsdata/tools/ 4913
04/01/13/16:35 /data/wsdata/tools/ j.jsp
04/01/13/16:35 /data/wsdata/tools/ j.jsp
04/01/13/16:35 /data/wsdata/tools/ j.jsp
04/01/13/16:35 /data/wsdata/tools/ j.jsp~
04/01/13/16:35 /data/wsdata/tools/ .j.jsp.swp

inotifywait命令参数

  • -m是要持续监视变化。

  • -r使用递归形式监视目录。

  • -q减少冗余信息,只打印出需要的信息。

  • -e指定要监视的事件列表。

  • --timefmt是指定时间的输出格式。

  • --format指定文件变化的详细信息。

可监听的事件

事件

描述

access

访问 ,读取文件。

modify

修改 ,文件内容被修改。

attrib

属性 ,文件元数据被修改。

move

移动 ,对文件进行移动操作。

create

创建 ,生成新文件

open

打开 ,对文件进行打开操作。

close

关闭 ,对文件进行关闭操作。

delete

删除 ,文件被删除。

其他Linux发行版安装方法可以参见:

https://github.com/rvoicilas/inotify-tools
http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
https://github.com/rvoicilas/inotify-tools/wiki#wiki-getting