linux监控目录变动
文件監控工具 inotify-tools
在Linux下載并安裝工具 inotify-tools 。
Ubuntu 下安裝:
sudo apt-get install inotify-tools- 1
例子
假設工程文件都放在 src/ 中,并且想監控此文件夾中的所有文件變更、創建、刪除操作,事件發生后可執行 mvn package 進行打包。
#!/bin/bash cd `dirname $0`rebuild() {local build_lock="build.lock"local pending_lock="pending.lock"test -f $build_lock && {echo "building is in process and waiting.." >&2touch $pending_lockexit 1}touch $build_lockecho -------------------- rebuildmvn packageecho -------------------- rebuild completerm $build_locktest -f $pending_lock && {rm $pending_lockrebuild &} }main() {local folder="src/"local file_4_changes="/tmp/watch"while inotifywait -e create,modify,delete -r $folder -o $file_4_changes; dorebuild &done }; main "$@"文件的變更歷史都在日志文件 /tmp/watch 中記錄。
///***************************************************************
使用inotify來監控文件變化。inotify是基于事件的監控,無需定時輪詢文件狀態,也無需做內核模塊hook系統調用,簡單可靠。比較好的實現是檢測到文件變化時,同時記錄下當前登陸的用戶以及當前用戶啟動的進程。不過inotify,需要kernel?2.6.13以上支持,RHEL5默認是有的,其它系統不確定。
比如:$ dnotify -rCDRM dragon_extfs/ -e echo hello&
監控dragon_extfs包括子目錄下所有改動,當發生(C創建,D刪除,R更名,M修改)等改變時執行命令echo hello
事件:
? -A, --access ? ? ? ? ? ? 目標目錄文件被訪問
? -M, --modify ? ? ? ? ? ? 目標目錄文件被改變
? -C, --create ? ? ? ? ? ? 目標目錄文件被創建
? -D, --delete ? ? ? ? ? ? 目標目錄文件被刪除
? -R, --rename ? ? ? ? ? ? 目標目錄文件被更名
? -B, --attrib ? ? ? ? ? ? 目標目錄文件被屬性修改
? ? ? ? ? ? ? ? ? ? ? ??
? -a, --all ? ? ? ? ? ? ? ?以上所有事件
參數:
? -e, --execute=COMMAND.. ?執行命令 一般情況下-e為最后一個參數,-e之后為命令argv
? -f, --file=FILE ? ? ? ? ?監控一個文件目錄列表
? -p, --processes=COUNT ? ?max number of commands to run at a time
? -q, --queue=DEPTH ? ? ? ?max depth of queue holding commands to be run
? -t, --times=COUNT ? ? ? ?exit after running the command COUNT times
? -o, --once ? ? ? ? ? ? ? same as `--times 1'
? -r, --recursive ? ? ? ? ?遞歸子目錄
? -b, --background ? ? ? ? 后臺運行
? -s, --silent ? ? ? ? ? ? 不打印信息
? ? ? --quiet ? ? ? ? ? ? ?同-s
? ? ? --help ? ? ? ? ? ? ? display this help and exit
? ? ? --version ? ? ? ? ? ?output version information and exit
總結
以上是生活随笔為你收集整理的linux监控目录变动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 15个最热门的GitHub库
- 下一篇: Linux下命令行压缩照片或图片的脚本