El Psy Congroo

Launchd cheatsheet

Launchd

launchd.info

LaunchDaemons - 启动时运行

  • 脚本位置
    • /System/Library/LaunchDaemons/ 系统专用
    • /Library/LaunchDaemons/
  • 脚本权限
    • 必须是root/wheel 644

LaunchAgents - 登录时运行

  • 脚本位置
    • /System/Library/LaunchAgents/ 系统专用
    • /Library/LaunchAgents/ 所有用户
    • ~/Library/LaunchAgents/ 当前用户
  • 脚本权限
    • 同上,用户目录不需要root

launchctl

load,unload,remove

plist格式

  • 启动时运行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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>com.my.auto_switch_kb</string>
<key>LimitLoadToSessionType</key>
<string>Aqua</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/python</string>
<string>/usr/local/bin/auto_switch_kb.py</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/auto_switch_kb.error</string>
<key>StandardOutPath</key>
<string>/tmp/auto_switch_kb.log</string>
</dict>
</plist>
  • 定时运行
1
2
<key>StartInterval</key>
<integer>60</integer>
  • 指定时间运行
1
2
3
4
5
6
7
8
9
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>45</integer>
<key>Hour</key>
<integer>13</integer>
<key>Day</key>
<integer>7</integer>
</dict>