Mac OS X系统中的launchctl命令

相关目录

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

使任务生效

将 plist 文件按需要放置在上文的相关目录中

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

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

例如

禁止apache启动

sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

还原apache启动

sudo launchctl load -wF /System/Library/LaunchDaemons/org.apache.httpd.plist

参数说明

  • -w Overrides the Disabled key and sets it to false. In previous versions, this option would modify the configuration file. Now the state of the Disabled key is stored elsewhere on-disk.覆盖plist中的Disabled key值,并将其设置为false
  • -F Force the loading of the plist. Ignore the Disabled key.强制载入plist文件,忽略Disabled key值