OSX 下面添加开机选项怎么加呢?这是个问题。系统提供了一个 LaunchDaemon 的玩意,用于提供这个功能。相关的命令是 launchd 和 launchctl 两个命令。
launch daemon 的配置文件是保存在 / System/Library/LaunchDaemons / 和 / Library/LaunchDaemons / 文件夹下。如果需要指定特定用户的配置,则可以放在~/Library/LaunchAgents、/Library/LaunchAgents 或者 / System/Library/LaunchAgents 下。区别为,是否用户登录之后执行还是内核初始化完成后执行。
具体的执行命令很简单
sudo launchctl load <plist file> #加载配置
sudo launchctl unload <file>
sudo launchctl start <bundleid>
sudo launchctl stop <bundleid>
下面是一个配置的具体示例,用 shadowsocks 为例:
<?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.example.shadowsocks</string><!-- 这个名字必须和文件名一致 -->
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/sslocal</string>
<string>-c</string>
<string>/Users/charles/Projects/shadowsocks-config/config.json</string>
</array>
<key>KeepAlive</key><!-- 后台保持运行 -->
<true/>
<key>RunAtLoad</key><!-- 加载时候运行 -->
<true/>
<key>UserName</key>
<string>charles</string><!-- 执行用户 -->
</dict>
</plist>
这个我们自己用户用的,放在~/Library/LaunchAgents 这个文件夹里。执行
launchctl load ~/Library/LaunchAgents/com.example.shadowsocks.plist
即可。
如果出现错误,则可以使用 plutil -lint 先判断文件格式是否正确。
参考文档