launchctlでjobが起動しない
macで定期実行をする際にcronではなくlaunchctlを使ってみたのでメモ
このような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>test</string>
<key>ProgramArguments</key>
<array>
<string>/tmp/test.sh</string>
</array>
<key>StartInterval</key>
<integer>1800</integer>
<key>StandardOutPath</key>
<string>/Users/username/log/test.log</string>
<key>StandardErrorPath</key>
<string>/Users/username/log/test.err.log</string>
</dict>
</plist>
test.shは適当にechoするだけ
echo 'testtest'
以下のようにしてjobを登録、起動
$ launchctl load ~/Library/LaunchAgents/test.plist $ launchctl start test
指定したログファイルを見てみようとするが、test.logもtest.err.logも存在せず。
/var/log/system.logを見てみるとこちらと同じエラーが出ていた
what does launchd status 78 mean?? why my user agent not running?? - Stack Overflow
permissionが...と言っているようなので調べてみると、ログディレクトリとして指定したディレクトリがrootがownerとなっていた。確かに考えてみればhomeの下にlogというディレクトリはもともと作ったりはしていなかったため、jobをstartした際にlaunchctl側で作成したのだと思われる。 ownerを自分のユーザにしてやり直したところ無事想定したログが出力されるようになった。
起動したprocessは自分のユーザとなっていたのだが、ディレクトリ作成のときはrootとなるようだ、、
ということで、ログファイルを指定した場合は出力先のディレクトリを自分で作成してからjobをstartすべき