简单开机启动
- 系统偏好设置->帐户->登陆项
- 执行程序启动目录
/System/Library/StartupItems
/Library/StartupItems/
可设置开机启动、唤醒启动
- 用户未登陆前就启动
/Library/LaunchDaemons
/System/Library/LaunchDaemons
- 用户登陆后启动
~/Library/LaunchAgents
/Library/LaunchAgents
/System/Library/LaunchAgents
唤醒时通常会重连wifi,而同时SystemConfiguration目录的内容会发生变化,从而触发sh文件
cd ~/Library/LaunchAgents
File: df.plist
在开机或者唤醒(wifi连接状态发生变化)时会触发
<?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>df</string>
<key>Program</key>
<string>/Users/df/wake.sh</string>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration</string>
</array>
<key>WorkingDirectory</key>
<string>/Users/df/</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
将文件设为可直接执行,将文件“显示简介”里的打开方式设置为shell
sudo chmod +x wake.sh
管理启动服务
launchctl load ~/Library/LaunchAgents/df.plist
launchctl start df
launchctl unload ~/Library/LaunchAgents/df.plist
launchctl stop df
- 卸载启动程序用rm是没用的,应该用“launchctl unload”
- 更改plist内容之后需要重新装载文件才能生效
- plist加载的程序会在后台运行,在前台是看不到的
关闭自启动程序
launchctl unload ~/Library/LaunchAgents/df.plist
常用的自启动目录
~/Library/LaunchAgents
/Library/LaunchAgents
禁用自启动程序
- cd ~/Library/LaunchAgents
- ls
- open org.getlantern.plist #用xcode打开org.getlantern.plist
- RunAtLoad —— No #禁止自启动
- cd /Library/LaunchAgents
- ls
- rm xx.plist #此目录无法修改RunAtLoad参数,只能删除