launchctl

2022-11-23

[[macOS]] 计划任务管理。

命令

  • launchctl list:列出
    • 78311 0 com.immwind
      • 78311 :进程号(- 表示未运行)
      • 0:成功退出
      • com.immwind:任务名
  • launchctl stop <plist>:停止
  • launchctl start <plist>:启动(启动前需先加载)
  • launchctl restart <plist>:重启
  • launchctl load <plist>:加载
  • launchctl unload <plist>:卸载指定任务(内容变更后需先卸载)

保存位置

  • ~/Library/LaunchAgents:用户自定义的任务(用户登录后才执行)
  • /Library/LaunchAgents:管理员定义的任务
  • /Library/LaunchDaemons:管理员定义的守护进程任务(系统启动即执行)
  • /System/Library/LaunchAgents :系统定义的任务
  • /System/Library/LaunchDaemons:系统定义的守护进程任务

执行周期

StartCalendarInterval 指定时间执行

<key>StartCalendarInterval</key>
<dict>
<key>Minute</key> # 分钟
<integer>30</integer>
<key>Hour</key> # 小时
<integer>9</integer>
<key>Weekday</key> # 周几(周日 0 或 7)
<integer>9</integer>
<key>Month</key> # 月
<integer>9</integer>
</dict>

StartInterval:间隔时间执行

# 间隔一小时执行
<key>StartInterval</key>
<integer>3600</integer>

说明

<?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.irs</string>
<key>KeepAlive</key> # 意外挂掉是否重启
<false/>
<key>RunAtLoad</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key> # 要执行的脚本
<array>
<string>/Users/immwind/Dropbox/scripts/irs_sync.sh</string>
</array>
<key>StartInterval</key>
<integer>360</integer>
</dict>
</plist>

参考