39
39

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(8)の使い方

Last updated at Posted at 2013-10-14

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分に実行する例

com.archinwateworks.src.brew.plist
<?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/LaunchAgentscom.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

launchctlの結果
[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";
	);
};

テスト投稿を兼ねて。

39
39
1

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
39
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?