9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

launchdのWatchPathsによるファイル監視

Last updated at Posted at 2015-11-23

WatchPathsまとめ

ぐぐっても古かったり不正確だったりするので.

WatchPathsは,launchdで使えるパラメータの一つです.
公式のリファレンスはこちらですが,見ても以下のような素気ない一文があるだけでよくわからないのが正直なところ.
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html

WatchPaths
This optional key causes the job to be started if any one of the listed paths are modified.

というわけで,どれぐらい使えるのか確認します.

前提

uraura@rosemary$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.1
BuildVersion:   15B42

El Capitanです.

準備

local.watchpaths-test.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>Label</key>
		<string>local.watchpaths-test</string>
		<key>Program</key>
		<string>/Users/uraura/work/ld/fired.sh</string>
		<key>WatchPaths</key>
		<array>
			<string>~/work/ld/watching</string>
		</array>
	</dict>
</plist>
fired.sh
# !/bin/sh

LOGFILE=/tmp/fired.log

{
	echo "$(date) fired!"
} >> "$LOGFILE" 2>&1

結果

やること|コマンド|結果
---|---|---|---
ディレクトリ生成|mkdir watching|fired1
パーミッション変更|chmod 777 watching|fired|
所有者変更|sudo chown nobody:nobody watching|fired|
ディレクトリの内容表示|ls watching|-|
ファイル作成|touch watching/foo|fired
ファイルのパーミッション変更|chmod 777 watching/foo|fired
ファイルの所有者変更|sudo chown nobody:nobody watching/foo|fired
ファイルに書き込み|date > watching/foo|fired
ファイルの内容表示|cat watching/foo|-
ファイルの削除|rm watching/foo|fired
サブディレクトリ作成|mkdir -p watching/sub1/sub2/sub3/sub4|fired
サブディレクトリ内にファイル生成|date > watching/sub1/sub2/sub3/sub4/bar|fired
サブディレクトリ内でパーミッション変更|chmod 777 watching/sub1/sub2/sub3/sub4/bar|fired
サブディレクトリ内のファイル削除|rm watching/sub1/sub2/sub3/sub4/bar|fired
サブディレクトリを一気に削除|rm -rf watching/sub1/sub2|fired
対象ディレクトリを削除|rm -rf watching|fired

以上より,WatchPathsに指定したディレクトリ以下はサブディレクトリを含め変更はすべて監視対象になるようです.
lscatで読みとるだけの操作では何も起こりませんでした.

サブディレクトリは監視対象にならない,というのをそこここで目にするのですが,昔の話?? :sake:


Nov 23 17:28:30 rosemary com.apple.xpc.launchd[1] (local.watchpaths-test): Service only ran for 0 seconds. Pushing respawn out by 10 seconds.

  1. /var/log/system.logにこんなログが出て,だいたい10秒後に2回目が発火しました.

9
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?