inotify-tools

最後更新: 2019-08-07

目錄

 

 


Install

 

# C7

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

inotifywatch -v mySecRules.conf

Establishing watches...
Total of 1 watches.
Finished establishing watches, now collecting statistics.

* 可以表 Ctrl+c 看結果

No events occurred.

# 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

 


 

Creative Commons license icon Creative Commons license icon