Skip to content

Instantly share code, notes, and snippets.

@kingtosh
Forked from kozy4324/launchd_memo.md
Created April 26, 2022 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingtosh/8f00b108e78e6fdfb9e0845f505b3bc5 to your computer and use it in GitHub Desktop.
Save kingtosh/8f00b108e78e6fdfb9e0845f505b3bc5 to your computer and use it in GitHub Desktop.
launchd, launchctlについて(導入編)

launchd, launchctl

man

$ man launchd
$ man launchctl
$ man launchd.plist
$ man plutil

daemon, agent

  • daemon => initdで実行されるようなデーモンプログラム
  • agent => crondで実行されるような定期実行プログラム

plistの置き場所と用途

~/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.

launchctl

load/unload

plistファイルに対して.

$ launchctl load /path/to/your.plist

start/stop

plistでload済みのjobはlabelを指定して起動/停止を行う.

$ launchctl start label

ただ OnDemand = falseもしくはKeepAlibe = trueだとloadした時点でjobが稼働するので、stopすると再起動する.

unload/remove

jobを除去する. unloadはplistを指定、removeであればlabelを指定でok.

$ launchctl unload /path/to/your.plist
$ launchctl remove label

StandardOutPath, StandardErrorPathを指定しないjobの出力先

謎.

launchctl.plist

標準出力/エラーの出力先

StandardOutPath, StandardErrorPathにファイルパスを設定する.

plistで設定するOnDemaondとKeepAlive

OnDemandは10.5以降でdeprecatedだからKeepAliveを使うべき、true/false以外に以下でjob継続するかが設定できる.

  1. 前回のexit code
  2. ネットワーク状況
  3. 特定ファイルの有無
  4. 他jobの有無

mongodをlaunchctlでdaemon化

my.mongod.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>my.mongod</string>
        <key>Program</key>
        <string>/path/to/mongod</string>
        <key>KeepAlive</key>
        <true/>
</dict>
</plist>

/Library/LaunchDaemons以下にぶちこめば次回システム起動時に読み込まれる.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment