launchctl 笔记

相关目录

  • /System/Library/LaunchDaemons 操作系统,用户登录前生效,以 root 身份执行任务
  • /System/Library/LaunchAgents 操作系统,用户登录后生效,以 root 身份执行任务
  • /Library/LaunchDaemons 系统管理员,用户登录前生效,以 root 身份执行任务
  • /Library/LaunchAgents 系统管理员,用户登录后生效,以 root 身份执行任务
  • ~/Library/LaunchAgents 当前用户,用户登录后生效,以当前登录用户身份执行任务

使任务生效

  1. 将 plist 文件按需要放置在上文的相关目录中
  2. launchctl load <plist path>

执行上述操作后任务会立即生效,且重启系统后也会自动生效。如果 plist 没有放在指定的目录,重启系统后不会再生效。

如果 plist 存在 Key Disabled,且其值为 true,launchctl load 会提示 nothing found to load,系统启动时也不会自动生效人物,如果要手动强制启动,忽略 Disabled 为 true 带来的影响,传递 -wF 参数给 launchctl 即可。

使任务失效

  1. launchctl unload <plist path>
  2. 将相关 plist 文件从原有目录中移除

执行上述操作后,任务会立即失效,plist 文件不会被移除,但是如果不将其从相关目录移走的话,重启系统后任务仍然会自动生效。

plist 文件范例

<?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>name.zhengwei.test</string>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/Wayne/Desktop/test.sh</string>
  </array>

  <key>Nice</key>
  <integer>1</integer>

  <key>StartInterval</key>
  <integer>10</integer>

  <key>RunAtLoad</key>
  <true/>

  <key>StandardErrorPath</key>
  <string>/tmp/test.err</string>

  <key>StandardOutPath</key>
  <string>/tmp/test.out</string>
</dict>
</plist>
  • XML 节点层级为 plist > dict > key / value
  • key /value 以节点的形式交替出现,value 支持的数据格式有 integer、string、array、true、false 等等
  • Label、Program和ProgramArguments 三个 Key 必须存在
  • Program 为要启动的应用程序或脚本路径
  • ProgramArguments 为传给依次传给 Program 的参数集合

参考链接

TODO

KeepAlive

This entry was posted in Mac OS. Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>