본문 바로가기
Linux

inotifywait / inotifywatch

by Nirah 2023. 3. 15.

- inotifywait - 파일시스템 이벤트 감지 및 로깅

설치
sudo yum install inotify-tools

파일시스템에서 create,modify 이벤트가 발생하면 event.txt에 로깅하기 
mkdir -p ~/inotify
inotifywait -o ~/event.txt -d --timefmt '%F %T' --format '%T %w %e %f' -m -r -e create,modify ~/inotify

cat ~/event.txt 
2019-05-19 19:22:49 /home1/irteamsu/inoti/ CREATE,ISDIR dkjfk
2019-05-19 19:22:49 /home1/irteamsu/inoti/ CREATE,ISDIR
2019-05-19 19:22:53 /home1/irteamsu/inoti/dkjfk/ CREATE
2019-05-19 19:22:53 /home1/irteamsu/inoti/dkjfk/ CREATE 1

파일 시스템 이벤트가 발생하면 메일을 보내보자
#!/bin/sh
inotifywait -o ~/event.txt -d --timefmt '%F %T' --format '%T %w %e %f' -m -r -e create,modify ~/inotify | while read NEWFILE
do
        echo "This is the body of your mail" | mailx -s "File ${NEWFILE} has been created" "yourmail@addresshere.tld"
done

참고
https://superuser.com/questions/956311/continuously-detect-new-files-with-inotify-tools-within-multiple-directories-r
https://linux.die.net/man/1/inotifywait
 

 

- inotifywatch - 60초 동안 파일시스템에서 발생한 이벤트 통계 확인하기

sudo inotifywatch -v -t 60 -r ./
total  access  modify  close_write  close_nowrite  open  create  delete  filename
885    621     0       0            132            132   0       0       ./volumes/test_wordpress_files/_data/wp-content/languages/
160    0       0       2            76             78    2       2       ./volumes/test_wordpress_files/_data/wp-content/
107    29      0       0            39             39    0       0       ./volumes/test_wordpress_files/_data/wp-content/themes/twentynineteen/
71     69      0       0            1              1     0       0       ./volumes/test_wordpress_files/_data/wp-includes/certificates/