inotify

来源:http://www.cnblogs.com/yinshoucheng-golden/archive/2017/02/06/6371728.html
-Advertisement-
Play Games

安裝inotify [root@server ~]# mkdir -p /home/oldboy/tools 安裝inotify-tools-3.14.tar.gz [root@server tools]# ls -l /proc/sys/fs/inotify/ #出現下麵三個表示支持inotify... ...


安裝inotify

  1. [root@server ~]# mkdir -p /home/oldboy/tools

安裝inotify-tools-3.14.tar.gz

  1. [root@server tools]# ls -l /proc/sys/fs/inotify/ #出現下麵三個表示支持inotify
  2. total 0
  3. -rw-r--r-- 1 root root 0 Feb 6 15:36 max_queued_events
  4. -rw-r--r-- 1 root root 0 Feb 6 15:36 max_user_instances
  5. -rw-r--r-- 1 root root 0 Feb 6 15:36 max_user_watches

解壓:

  1. [root@server tools]# tar zxf inotify-tools-3.14.tar.gz

編譯安裝:

  1. [root@server tools]# cd inotify-tools-3.14
  2. [root@server inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
  3. [root@server inotify-tools-3.14]# echo $?
  4. 0
  5. [root@server inotify-tools-3.14]# make && make install
  6. [root@server inotify-tools-3.14]# echo $? #0表示安裝正確
  7. 0
  8. [root@server tools]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools #創建軟連接
  9. [root@server tools]# ls -l /usr/local/inotify-tools/
  10. total 16
  11. drwxr-xr-x. 2 root root 4096 Feb 6 15:43 bin #inotify執行命令(二進位)
  12. drwxr-xr-x. 3 root root 4096 Feb 6 15:42 include #inotify程式所需要的頭文件
  13. drwxr-xr-x. 2 root root 4096 Feb 6 15:43 lib #動態鏈接的庫文件
  14. drwxr-xr-x. 4 root root 4096 Feb 6 15:42 share #幫助文檔

 

  1. [root@server inotify-tools]# ll bin/
  2. total 88
  3. -rwxr-xr-x. 1 root root 44287 Feb 6 15:43 inotifywait
  4. -rwxr-xr-x. 1 root root 41409 Feb 6 15:43 inotifywatch

inotifywait:在被監控的文件或目錄上等待特定文件系統事件(open、close、delete等)發生,執行後處於阻塞轉態,適合在shell腳本中使用。

inotifywatch:收集被監控的文件系統使用度統計數據,指文件系統事件發生的次數統計。

inotifywait命令常用參數

  1. [root@server inotify-tools]# ./bin/inotifywait --help
  2. inotifywait 3.14
  3. Wait for a particular event on a file or set of files.
  4. Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
  5. Options:
  6.         -h|--help Show this help text.
  7.         @<file> Exclude the specified file from being watched.
  8.         --exclude <pattern>
  9.                         Exclude all events on files matching the
  10.                         extended regular expression <pattern>.
  11.         --excludei <pattern>
  12.                         Like --exclude but case insensitive. #排除文件或目錄時,不區分大小寫
  13.         -m|--monitor Keep listening for events forever. Without
  14.                         this option, inotifywait will exit after one
  15.                         event is received. #始終保持事件監聽狀態
  16.         -d|--daemon Same as --monitor, except run in the background
  17.                         logging events to a file specified by --outfile.
  18.                         Implies --syslog.
  19.         -r|--recursive Watch directories recursively. #遞歸查詢目錄
  20.         --fromfile <file>
  21.                         Read files to watch from <file> or `-' for stdin.
  22.         -o|--outfile <file>
  23.                         Print events to <file> rather than stdout.
  24.         -s|--syslog Send errors to syslog rather than stderr.
  25.         -q|--quiet Print less (only print events). #列印很少的信息,僅僅列印監控事件的信息
  26.         -qq Print nothing (not even events).
  27.         --format <fmt> Print using a specified printf-like format
  28.                         string; read the man page for more details.
  29.         --timefmt <fmt> strftime-compatible format string for use with
  30.                         %T in --format string. #指定事件輸出的格式
  31.         -c|--csv Print events in CSV format.
  32.         -t|--timeout <seconds>
  33.                         When listening for a single event, time out after
  34.                         waiting for an event for <seconds> seconds.
  35.                         If <seconds> is 0, inotifywait will never time out.
  36.         -e|--event <event1> [ -e|--event <event2> ... ]
  37.                 Listen for specific event(s). If omitted, all events are
  38.                 listened for. #監控事件
  39.  
  40. Exit status:
  41.         0 - An event you asked to watch for was received.
  42.         1 - An event you did not ask to watch for was received
  43.               (usually delete_self or unmount), or some error occurred.
  44.         2 - The --timeout option was given and no events occurred
  45.               in the specified interval of time.
  46.  
  47. Events:
  48.         access file or directory contents were read
  49.         modify file or directory contents were written
  50.         attrib file or directory attributes changed
  51.         close_write file or directory closed, after being opened in
  52.                         writeable mode
  53.         close_nowrite file or directory closed, after being opened in
  54.                         read-only mode
  55.         close file or directory closed, regardless of read/write mode
  56.         open file or directory opened
  57.         moved_to file or directory moved to watched directory
  58.         moved_from file or directory moved from watched directory
  59.         move file or directory moved to or from watched directory
  60.         create file or directory created within watched directory
  61.         delete file or directory deleted within watched directory
  62.         delete_self file or directory was deleted
  63.         unmount file system containing file or directory unmounted

測試一:

視窗一:

  1. [root@server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data #create創建(監控創建事件)
  2. 06/02/17 16:13 /data/test.txt
  3. 06/02/17 16:13 /data/test2.txt
  4. 06/02/17 16:14 /data/1.txt
  5. 06/02/17 16:14 /data/2.txt
  6. 06/02/17 16:14 /data/3.txt
  7. 06/02/17 16:14 /data/4.txt

視窗二:

  1. [root@server ~]# cd /data/
  2. [root@server data]# ls
  3. [root@server data]# touch test.txt
  4. [root@server data]# touch test2.txt
  5. [root@server data]# touch {1..4}.txt

測試二:

視窗一:

  1. [root@server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data #delete刪除(監控刪除事件)
  2. 06/02/17 16:17 /data/test.txt
  3. 06/02/17 16:17 /data/test2.txt

視窗二:

  1. [root@server data]# rm -f test.txt
  2. [root@server data]# rm -f test2.txt

同時監控多個事件,事件之間用逗號分隔。

  1. /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data

測試:

視窗一:

  1. [root@server scripts]# cat inotify.sh
  2. #!/bin/bash
  3. inotify=/usr/local/inotify-tools/bin/inotifywait
  4. $inotify -mrq --format '%w%f' -e create,close_write,delete /data \
  5. |while read file
  6. do
  7.   cd / &&
  8.   rsync -az ./data --delete [email protected]::backup/ \
  9.   --password-file=/etc/rsync.password
  10. done
  11. [root@server scripts]# sh -x inotify.sh
  12. + inotify=/usr/local/inotify-tools/bin/inotifywait
  13. + read file
  14. + /usr/local/inotify-tools/bin/inotifywait -mrq --format %w%f -e create,close_write,delete /data
  15. + cd /
  16. + rsync -az ./data --delete [email protected]::backup/ --password-file=/etc/rsync.password
  17. + read file
  18. + cd /
  19. + rsync -az ./data --delete [email protected]::backup/ --password-file=/etc/rsync.password
  20. + read file

視窗二:

  1. [root@server data]# touch oldboy.txt
  2. [root@server data]# touch test.log

備份伺服器:

  1. [root@backup backup]# ls
  2. data
  3. [root@backup backup]# tree
  4. .
  5. └── data
  6.     ── 2.txt
  7.     ── 3.txt
  8.     ── 4.txt
  9.     ── oldboy.txt
  10.     ── test.log
  11.     └── test.txt

 

  1. [root@server scripts]# /bin/sh /server/scripts/inotify.sh & #放入後臺執行

寫入rc.local

應用場景:10—300k小文件併發200—300,不會有延遲。

關鍵參數說明:

在/proc/sys/fs/inotify目錄下有三個文件,對inotify機制有一定的限制。

max_user_watches:設置inotifywait或inotifywatch命令可以監視的文件數量(單進程)。

max_user_instances:設置每個用戶可以運行的inotifywait或inotifywatch命令的進程數。

max_queued_events:設置inotify實例事件(event)隊列可容納的事件數量。

  1. [root@server scripts]# cat /proc/sys/fs/inotify/max_user_watches # 修改為50000000
  2. 8192
  3. [root@server scripts]# cat /proc/sys/fs/inotify/max_user_instances #
  4. 128
  5. [root@server scripts]# cat /proc/sys/fs/inotify/max_queued_events #修改為50000000
  6. 16384

每秒200個文件併發,數據同步幾乎無延遲。

inotify優點:

實時數據同步。

inotify缺點:

1、併發如果大於200個文件(10—100K),同步會有延遲。

2、監控到事件後,調用rsync同步是單進程的(加&併發),sersync多進程同步。

sersync的功能:

1、配置文件。

2、真正的守護進程socker。

3、可以對失敗文件定時重傳(定時任務)。

4、第三方的http介面。

5、預設多線程同步。

高併發數據實時同步方案:

1、文件級別:inotify(sersync)+rsync。

2、文件系統級別:drbd。

3、第三方軟體的同步功能:mysql同步、oracle、mongodb。

4、程式雙寫。

5、業務邏輯解決。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 環境:Oracle 11.2, CEntOS 6.6, VMware 原因:磁碟I/O性能低效,VMware虛擬機備份占用大量磁碟I/O吞吐能力 現象:最後的alert日誌內容 Fri Feb 03 17:38:57 2017********************* ATTENTION: **** ...
  • SSH 1、ssh是安全的加密協議,用於遠程連接linux伺服器。 2、ssh預設埠是22,安全協議版本ssh2。 3、ssh服務端主要包含兩個服務功能ssh遠程連接,sftp服務。 4、linux ssh客戶端包含ssh遠程連接命令,以及遠程拷貝scp命令等。 SSH服務認證類型 基於口令的安全... ...
  • git clone 遠程地址時候出現 "fatal: protocol error: bad line length character: No This" 錯誤 在stackoverflow上查到的是修改~/.bashrc文件採用 echo "錯誤信息" 1&2>/dev/null 來忽略錯誤提示 ...
  •  ...
  • 部署環境:VM虛擬機 操作系統:CentOS-6.8-x64 IP地址:192.168.31.91Mysql資料庫版本:5.6.34 Cmake軟體包版本:3.5.2Nginx軟體包版本:1.10.2 PHP軟體包版本:5.6.30 依賴軟體包版本:libiconv-1.14.tar.gz 免費開源 ...
  • 本文主要介紹瞭如何在Linux環境下,在QT中如何引用Shark machine learning library的方法。 ...
  • 為了方便與UCOS對比,順序按照UCOS那篇編寫。 0、一些移植、系統相關 1、框架寫法(個人習慣相關) 1-1、main 函數里創建一個開始任務 1-2、開始任務里,創建我們要運行的多個任務 2、任務創建、掛起、刪除 2-0、相關配置 2-1、任務創建(動態) 2-2、任務掛起 2-3、任務解掛 ...
  • 方法及其簡單,只需要兩步即可: 1、第一步: 打開終端,輸入sudo su命令。 –此處的密碼為普通用戶的密碼,也就是開機時輸入的密碼。 2、第二步: 直接sudo passwd root就重置了roor密碼了。 此時輸入新的密碼即可,個人建議密碼最好字母加數字相結合。 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...