Press "Enter" to skip to content

Mac OS 增加开机自启动脚本

OSX 原生使用一个叫launchd的进程任务管理器。

所以呢,你创建个launchctl的守护任务,就可以完全控制你启动脚本,让他执行一次,或者作为守护进程一直运行。

具体步骤:

  1. 根据苹果协议( instructions in the Apple Dev docs here  )创建一个 .plist 文档
  2. 把这个文件放到 ~/Library/LaunchAgents目录下面
  3. 重新登录,或者手动运行  launchctl load [filename.plist]

例如,你可以创建这样一个文件:

~/Library/LaunchAgents/com.user.loginscript.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>com.user.loginscript</string>
   <key>ProgramArguments</key>
   <array><string>/path/to/executable/script.sh</string></array>
   <key>RunAtLoad</key>
   <true/>
</dict>
</plist>

记得 chmod a+x /path/to/executable/script.sh 给脚本可执行权限

错误日志在 /var/log/system.log

参考资料:https://stackoverflow.com/questions/6442364/running-script-upon-login-mac

发表回复

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