Help us understand the problem. What is going on with this article?

launchd(8)の使い方

More than 5 years have passed since last update.

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";
    );
};

テスト投稿を兼ねて。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした