Using launchd
to watch a filesystem directory and upon create/change to a file with the directory, execute a command.
For this example:
- Watching directory of:
/path/to/watch
. - Upon file change, execute:
/path/to/command argument1 argument2
.
Copy, or symlink example.plist
under the directory $HOME/Library/LaunchAgents
:
$ ln -fs "./example.plist "$HOME/Library/LaunchAgents"
Load example.plist
into launchd
:
launchctl load "$HOME/Library/LaunchAgents/example.plist"
The example.plist
can be removed by running:
launchctl unload "$HOME/Library/LaunchAgents/example.plist"
then deleting example.plist
.
- https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
- https://mayeu.me/post/how-to-trigger-any-action-when-a-file-or-folder-changes-on-macos-on-the-cheap/
- https://apple.stackexchange.com/questions/40705/monitor-a-folder-for-changes-and-run-a-command-when-a-change-is-detected/40727#40727