最近在除錯乙個filebeat程式時需要製造一些log,我是直接使用vim直接對檔案開啟然後直接儲存的。
但是有個奇怪的現象:每次寫入一行新的日誌,filebeat都會將整個檔案的內容又重新進行上報一遍導致日誌上傳重複,同時觀察到filebeat的檔案採集狀態檔案registry
都會進行增加乙個重複的檔案。
操作一番後以為是程式的問題,最後才反應過來,最終使用echo "test some log">>test.log
追加的方式發現不會有此問題。
同時看了下兩者的inode
資訊,確認了是因為vim
會改變檔案的inode(乙個新的檔案),echo則不會:
1、檢視檔案inode為908488
[root@localhost work]# stat test.log
檔案:"test.log"
大小:0 塊:0 io 塊:4096 普通空檔案
許可權:(0644/-rw-r--r--) uid:( 0/ root) gid:( 0/ root)
環境:unconfined_u:object_r:admin_home_t:s0
最近訪問:2020-05-10 04:38:39.399791808 -0400
最近更改:2020-05-10 04:38:39.399791808 -0400
最近改動:2020-05-10 04:38:39.399791808 -0400
3、而用echo命令修改看到inode資訊並沒有改變, 908490[root@localhost work]# vi test.log
[root@localhost work]# stat test.log
檔案:"test.log"
大小:7 塊:8 io 塊:4096 普通檔案
許可權:(0644/-rw-r--r--) uid:( 0/ root) gid:( 0/ root)
環境:unconfined_u:object_r:admin_home_t:s0
最近訪問:2020-05-10 04:39:01.950055784 -0400
最近更改:2020-05-10 04:39:01.950055784 -0400
最近改動:2020-05-10 04:39:01.953305882 -0400
其實通過inode的改變以及可以猜測出在使用vi/vim進行編輯的時候,檔案以及變為了乙個新的檔案,這裡我主要通過[root@localhost work]# echo "test some log">>test.log
[root@localhost work]# stat test.log
檔案:"test.log"
大小:21 塊:8 io 塊:4096 普通檔案
許可權:(0644/-rw-r--r--) uid:( 0/ root) gid:( 0/ root)
環境:unconfined_u:object_r:admin_home_t:s0
最近訪問:2020-05-10 04:39:01.950055784 -0400
最近更改:2020-05-10 04:41:16.794459151 -0400
最近改動:2020-05-10 04:41:16.794459151 -0400
inotifywait
來看一下,在進行vim編輯的時候,檔案會產生什麼時間
一開始本來打算使用golang中inotifywait命令:github.com/fsnotify/fsnotify
去看的,但是發現包中對event進行了合併,不夠原始。所以才直接使用inotifywait
來看
inotifywait -rm test.log
可以看到對檔案進行了[root@localhost work]# inotifywait -rm test.log
setting up watches. beware: since -r was given, this may take a while!
watches established.
test.log open
test.log access
test.log close_nowrite,close
test.log move_self
test.log attrib
test.log delete_self
delete_self
注意當再次進行編輯的時候,已經無法監聽到了,因為原有檔案已經被刪除,需要重新監聽
2、使用echo進行追加寫入的結果
3、監聽整個目錄,可以看到更多資訊[root@localhost work]# inotifywait -rm test.log
setting up watches. beware: since -r was given, this may take a while!
watches established.
test.log open
test.log modify
test.log close_write,close
可以看到在進行檔案儲存時,將原有test.log 移動為test.log~,並新建立乙個檔案test.log,最後對test.log~備份檔案和.test.log.swp緩衝區檔案進行刪除[root@localhost work]# inotifywait -rm ./dir/
setting up watches. beware: since -r was given, this may take a while!
watches established.
# 當進行檔案寫入時
./dir/ open test.log
./dir/ create .test.log.swp
./dir/ open .test.log.swp
./dir/ create .test.log.swx
./dir/ open .test.log.swx
./dir/ close_write,close .test.log.swx
./dir/ delete .test.log.swx
./dir/ close_write,close .test.log.swp
./dir/ delete .test.log.swp
./dir/ create .test.log.swp
./dir/ open .test.log.swp
./dir/ modify .test.log.swp
./dir/ attrib .test.log.swp
./dir/ access test.log
./dir/ close_nowrite,close test.log
./dir/ modify .test.log.swp
# 當進行檔案儲存是
./dir/ create 4913
./dir/ open 4913
./dir/ attrib 4913
./dir/ close_write,close 4913
./dir/ delete 4913
./dir/ moved_from test.log
./dir/ moved_to test.log~
./dir/ create test.log
./dir/ open test.log
./dir/ modify test.log
./dir/ close_write,close test.log
./dir/ attrib test.log
./dir/ modify .test.log.swp
./dir/ delete test.log~
./dir/ close_write,close .test.log.swp
./dir/ delete .test.log.swp
vim的儲存檔案和退出命令
vim的儲存檔案和退出命令 命令簡單說明 w 儲存編輯後的檔案內容,但不退出vim編輯器。這個命令的作用是把記憶體緩衝區中的資料寫到啟動vim時指定的檔案中。w 強制寫檔案,即強制覆蓋原有檔案。如果原有檔案的訪問許可權不允許寫入檔案,例如,原有的檔案為唯讀檔案,則可使用這個命令強制寫入。但是,這種命...
vim的儲存檔案和退出命令
vim 的儲存檔案和退出命令命令 簡單說明 w 儲存編輯後的檔案內容,但不退出 vim編輯器。這個命令的作用是把記憶體緩衝區中的資料寫到啟動 vim時指定的檔案中。w 強制寫檔案,即強制覆蓋原有檔案。如果原有檔案的訪問許可權不允許寫入檔案,例如,原有的檔案為唯讀檔案,則可使用這個命令強制寫入。但是,...
vim的儲存檔案和退出命令
命令 簡單說明 w 儲存編輯後的檔案內容,但不退出vim編輯器。這個命令的作用是把記憶體緩衝區中的資料寫到啟動vim時指定的檔案中。w 強制寫檔案,即強制覆蓋原有檔案。如果原有檔案的訪問許可權不允許寫入檔案,例如,原有的檔案為唯讀檔案,則可使用這個命令強制寫入。但是,這種命令用法僅當使用者是檔案的屬...