在Ubuntu系統中配置和使用inotify主要涉及安裝inotify-tools以及編寫和運行監控腳本。以下是詳細的步驟:
sudo apt update
sudo apt install inotify-tools
monitor.sh的腳本文件。nano monitor.sh
#!/bin/bash
# 監控的文件路徑
FILE_PATH="/path/to/your/file"
# 使用inotifywait監控文件變化
inotifywait -m -e modify,attrib,close_write,move,create,delete "$FILE_PATH" | while read -r directory events filename; do
# 當文件發生變化時,觸發通知
notify-send "File Changed" "The file $filename has been changed."
done
chmod +x monitor.sh
./monitor.sh
-m:持續監控模式,不斷檢查文件變化。-e:指定要監控的事件類型,例如modify(修改)、attrib(屬性變化)、close_write(寫入關閉)、move(移動)、create(創建)、delete(刪除)。notify-send:發送桌面通知。-r選項遞歸監控指定目錄及其所有子目錄。--format參數自定義輸出格式。0x00000001|0x00000002|0x00000004表示監控創建、修改和刪除事件。uname -r命令檢查內核版本。/proc/sys/fs/inotify/max_user_watches文件來增加限制。通過以上步驟,你可以在Ubuntu系統上成功配置和使用inotify來監控文件系統的變化。