OSXにcrontab -e
がなくて困ったのでメモ。
launchd(8)とは
- OSXで導入された
init(8)
に変わるアレ
DESCRIPTION
launchd manages processes, both for the system as a whole and for individual users. The primary and preferred interface to launchd is via the launchctl(1) tool which (among other options) allows the user or administrator to load and unload jobs. Where possible, it is preferable for jobs to launch on demand based on criteria specified in their respective configuration files.
During boot launchd is invoked by the kernel to run as the first process on the system and to further bootstrap the rest of the system.
You cannot invoke launchd directly.
launchd.plist(5)の置き場所
Path | Description |
---|---|
~/Library/LaunchAgents | Per-user agents provided by the user. |
/Library/LaunchAgents | Per-user agents provided by the administrator. |
/Library/LaunchDaemons | System-wide daemons provided by the administrator. |
/System/Library/LaunchAgents | Per-user agents provided by Mac OS X. |
/System/Library/LaunchDaemons | System-wide daemons provided by Mac OS X. |
launchd.plist(5)の書き方サンプル
$ brew update
を毎日2時50分に実行する例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTDPLIST1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.archinwaterworks.src.brew</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/brew</string>
<string>update</string>
</array>
<key>WorkingDirectory</key>
<string>/Users/archinwater</string>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>2</integer>
<key>Minute</key>
<integer>30</integer>
</dict>
</dict>
</plist>
- ファイル名は適当でもいいっぽいが、前にならえということで。
- 今回はユーザ権限で動かせばよいコマンドなので、
~/Library/LaunchAgents
にcom.archinwaterworks.src.brew.plist
として保存する。後続のlaunchctl(5)
は拡張子を見てるっぽいので、拡張子を間違えないように注意。 - 設定した時刻に端末が寝ていても、再び起きたときに動くらしいのです。
-
/var/log/system.log
に動作した結果は出てくる模様。
出力先の指定もできます。
Option | Description |
---|---|
StandardInPath | This optional key specifies what file should be used for data being supplied to stdin when using stdio(3). |
StandardOutPath | This optional key specifies what file should be used for data being sent to stdout when using stdio(3). |
StandardErrorPath | This optional key specifies what file should be used for data being sent to stderr when using stdio(3). |
see also: launchd(8)
, launchd.plist(5)
launchd.plist(5)をlaunchctl(1)で登録する
$ launchctl load ~/Library/LaunchAgents/com.archinwaterworks.src.brew.plist
エラーが出なければ登録されていることになる(らしい)。というのは不親切なので、以下のコマンドで確認できる。
$ launchctl list com.archinwaterworks.src.brew
[11:35] archinwater % launchctl list com.archinwaterworks.src.brew
{
"Label" = "com.archinwaterworks.src.brew";
"LimitLoadToSessionType" = "Aqua";
"OnDemand" = true;
"LastExitStatus" = 0;
"TimeOut" = 30;
"ProgramArguments" = (
"/usr/local/bin/brew";
"update";
);
};
テスト投稿を兼ねて。