inotify-tools

最後更新: 2019-08-07

 

 

 


Install

 

# Install

yum install inotify-tools

獲得

  • /usr/bin/inotifywatch - wait for changes to files using inotify
  • /usr/bin/inotifywait - gather filesystem access statistics using inotify

 


inotifywatch

 

Learn by example

# watch a folder "/tmp"

inotifywatch -v -e access -e modify -t 120 -r /tmp

-v, --verbose   # information on standard error

-e <event>     # Listen for specific event(s) only.

-t <seconds>   # Listen only for the specified amount of seconds ( 要過了它才有 report )

-r, --recursive   # Watch  all  subdirectories

EVENTS

  • access
  • modify
  • open          # touch it
  • move
  • create
  • delete
  • .......

 


inotifywait

 

It can either exit once an event occurs, or continually execute and output events as they occur.

Example: Run a command when a directory's contents are updated

#!/bin/sh
dir1=/path/to/A/
while inotifywait -qq -r -e modify "$dir1"; do
    /run/backup/to/B
done

-qq switch is completely silent

-r is recursive (if needed)

-e is the event to monitor

EXIT STATUS

0 an event occurred

1 An error occurred in execution of the program or an event occurred which was not being listened for.

   i.e. "-e modify" 時 file 被 delete 了

2 "-t" option was used and an event did not occur