Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

Watchman: เครื่องมือตรวจสอบการเปลี่ยนแปลงไฟล์และไดเรกทอรีอย่างมีประสิทธิภาพ

Watchman เป็น Open Source และ Cross-platform File Watching Service ที่เฝ้าดูไฟล์และบันทึกหรือดำเนินการเมื่อพวกมันเปลี่ยนแปลง มันพัฒนาโดย Facebook และรันบน Linux, OS X, FreeBSD และ Solaris มันรันในรูปแบบ Client-Server Model และใช้ Inotify Utility ของ Linux Kernel เพื่อให้ Notification ที่ทรงพลังมากขึ้น

แนวคิดที่มีประโยชน์ของ Watchman

  • มัน Watch (เฝ้าดู) Directory Trees หนึ่งหรือหลายแห่ง แบบ Recursive
  • แต่ละ Watched Directory เรียกว่า Root
  • มันสามารถกำหนดค่าได้ผ่าน Command-line หรือ Configuration File ที่เขียนใน JSON Format
  • มันบันทึกการเปลี่ยนแปลงลง Log Files
  • รองรับ Subscription ต่อการเปลี่ยนแปลงไฟล์ที่เกิดขึ้นใน Root
  • อนุญาตให้คุณ Query Root สำหรับการเปลี่ยนแปลงไฟล์ตั้งแต่ครั้งล่าสุดที่คุณตรวจสอบ หรือ Current State ของ Tree
  • มันสามารถ Watch โปรเจกต์ทั้งหมดได้

ในบทความนี้ เราจะอธิบายวิธีติดตั้งและใช้ watchman เพื่อ Watch (Monitor) ไฟล์และบันทึกเมื่อพวกมันเปลี่ยนแปลงใน Linux เราจะสาธิตสั้นๆ วิธี Watch Directory และเรียกใช้ Script เมื่อมันเปลี่ยนแปลง

ติดตั้ง Watchman File Watching Service ใน Linux

เราจะติดตั้ง Service watchman จาก Source ดังนั้น ขั้นแรกติดตั้ง Dependencies ที่จำเป็น libssl-dev, autoconf, automake libtool, setuptools, python-devel และ libfolly โดยใช้คำสั่งต่อไปนี้บน Linux Distribution ของคุณ

----------- On Debian/Ubuntu -----------
$ sudo apt install autoconf automake build-essential python-setuptools python-dev libssl-dev libtool
----------- On RHEL/CentOS -----------
# yum install autoconf automake python-setuptools python-devel openssl-devel libssl-devel libtool
# yum groupinstall 'Development Tools'
----------- On Fedora -----------
$ sudo dnf install autoconf automake python-setuptools openssl-devel libssl-devel libtool
$ sudo dnf groupinstall 'Development Tools'

เมื่อติดตั้ง Dependencies ที่จำเป็นเสร็จแล้ว คุณสามารถเริ่ม Build Watchman ได้ โดยดาวน์โหลด GitHub Repository ของมัน เข้า Local Repository Configure, Build และ Install ด้วยคำสั่งต่อไปนี้:

$ git clone https://github.com/facebook/watchman.git
$ cd watchman
$ git checkout v4.9.0
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

การ Watch ไฟล์และไดเรกทอรีด้วย Watchman ใน Linux

Watchman สามารถกำหนดค่าได้ 2 วิธี: (1) ผ่าน Command-line ขณะ Daemon ทำงานใน Background หรือ (2) ผ่าน Configuration File ที่เขียนใน JSON Format

เพื่อ Watch Directory (เช่น ~/bin) สำหรับการเปลี่ยนแปลง ให้รันคำสั่งต่อไปนี้:

$ watchman watch ~/bin/
Watchman: เครื่องมือตรวจสอบการเปลี่ยนแปลงไฟล์และไดเรกทอรีอย่างมีประสิทธิภาพWatch a Directory in Linux

คำสั่งต่อไปนี้เขียน Configuration File ชื่อ state ภายใต้ /usr/local/var/run/watchman/<username>-state/ ใน JSON Format รวมถึง Log File ชื่อ log ในตำแหน่งเดียวกัน

คุณสามารถดูไฟล์ทั้งสองได้โดยใช้คำสั่ง Cat ตามที่แสดง

$ cat /usr/local/var/run/watchman/aaronkilik-state/state
$ cat /usr/local/var/run/watchman/aaronkilik-state/log

คุณยังสามารถกำหนด Action ที่จะ Trigger เมื่อ Directory ที่ Watch อยู่มีการเปลี่ยนแปลงได้ ตัวอย่างเช่น ในคำสั่งต่อไปนี้ ‘test-trigger‘ คือชื่อของ Trigger และ ~bin/pav.sh คือ Script ที่จะถูกเรียกใช้เมื่อตรวจพบการเปลี่ยนแปลงใน Directory ที่ Monitor อยู่

เพื่อวัตถุประสงค์ทดสอบ Script pav.sh เพียงแค่สร้างไฟล์พร้อม Timestamp (เช่น file.$time.txt) ภายใน Directory เดียวกับที่ Script จัดเก็บ

time=`date +%Y-%m-%d.%H:%M:%S`
touch file.$time.txt

บันทึกไฟล์ และทำให้ Script Executable ตามที่แสดง

$ chmod +x ~/bin/pav.sh

เพื่อเปิดใช้งาน Trigger ให้รันคำสั่งต่อไปนี้:

$ watchman -- trigger ~/bin 'test-trigger' -- ~/bin/pav.sh
Watchman: เครื่องมือตรวจสอบการเปลี่ยนแปลงไฟล์และไดเรกทอรีอย่างมีประสิทธิภาพCreate a Trigger on Directory

เมื่อคุณ Execute watchman เพื่อเฝ้าดู Directory มันจะถูกเพิ่มเข้า Watch List และเพื่อดู ให้รันคำสั่งต่อไปนี้:

$ watchman watch-list
Watchman: เครื่องมือตรวจสอบการเปลี่ยนแปลงไฟล์และไดเรกทอรีอย่างมีประสิทธิภาพView Watch List

เพื่อดู Trigger List สำหรับ Root ให้รันคำสั่งต่อไปนี้ (แทนที่ ~/bin ด้วยชื่อ Root)

$ watchman trigger-list ~/bin
Watchman: เครื่องมือตรวจสอบการเปลี่ยนแปลงไฟล์และไดเรกทอรีอย่างมีประสิทธิภาพShow Trigger List for a Root

จากการกำหนดค่าข้างต้น ทุกครั้งที่ Directory ~/bin เปลี่ยนแปลง ไฟล์อย่าง file.2019-03-13.23:14:17.txt จะถูกสร้างภายใน และคุณสามารถดูได้ด้วยคำสั่ง ls

$ ls
Watchman: เครื่องมือตรวจสอบการเปลี่ยนแปลงไฟล์และไดเรกทอรีอย่างมีประสิทธิภาพTest Watchman Configuration

การ Uninstall Watchman Service ใน Linux

หากคุณต้องการ Uninstall watchman ให้เข้า Source Directory และรันคำสั่งต่อไปนี้:

$ sudo make uninstall
$ cd '/usr/local/bin' && rm -f watchman
$ cd '/usr/local/share/doc/watchman-4.9.0 ' && rm -f README.markdown

สำหรับข้อมูลเพิ่มเติม เยี่ยมชม Watchman Github Repository: https://github.com/facebook/watchman.

คุณอาจชอบอ่านบทความที่เกี่ยวข้องต่อไปนี้

  1. Swatchdog – Simple Log File Watcher in Real-Time in Linux
  2. 4 Ways to Watch or Monitor Log Files in Real Time
  3. fswatch – Monitors Files and Directory Changes in Linux
  4. Pyintify – Monitor Filesystem Changes in Real Time in Linux
  5. Inav – Watch Apache Logs in Real Time in Linux

Watchman เป็น Open Source File Watching Service ที่เฝ้าดูไฟล์และบันทึก หรือ Trigger Actions เมื่อพวกมันเปลี่ยนแปลง ใช้ Feedback Form ด้านล่างเพื่อถามคำถามหรือแบ่งปันความคิดกับเรา