如何在osx上实现linux的inotify-tools shell方法

Bil*_*oon 7 macos file watch inotify-tools

要监视linux中的文件,我可以使用这样的inotify-tools

#!/bin/bash

# with inotify-tools installed, watch for modification of file passed as first param
while inotifywait -e modify $1; do
        # do something here
done
Run Code Online (Sandbox Code Playgroud)

但是我如何在OSX中实现这一目标?

Pet*_*kas 6

如果要将其包装到Python脚本中,可以使用Watchdog,它适用于Linux和OSX.

https://pypi.python.org/pypi/watchdog

以下是用看门狗替换pyinotify的样子:

https://github.com/raphdg/baboon/commit/2c115da63dac16d0fbdc9b45067d0ab0960143ed

Watchdog还有一个shell实用程序,名为watchmedo:

watchmedo shell-command \
    --patterns="*.py;*.txt" \
    --recursive \
    --command='echo "${watch_src_path}"' \
    .
Run Code Online (Sandbox Code Playgroud)