MacOS设置开机自启

使用Launchctl命令

1
2
3
4
//设置开机自启
sudo launchctl load /path/service.plist
//关闭开机自启
sudo launchctl unload /path/service/plist

一般存放路径

  • /Library/LaunchDaemons/ 由管理员定义的守护进程任务项
  • /Library/LaunchAgents/ 由管理员为用户定义的任务项
  • ~/Library/LaunchAgents/ 由用户自己定义的任务项
  • /System/Library/LaunchAgents 由Mac OS X为用户定义的任务项
    Agents文件夹下的plist是需要用户登录后,才会加载的,而Daemons文件夹下得plist是只要开机,可以不用登录就会被加载

示例plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?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>dnsproxy</string>
<key>ProgramArguments</key>
<array>
<string>/opt/bin/dnsproxy</string>
<string>-l</string>
<string>127.0.0.1</string>
<string>-p</string>
<string>53</string>
<string>-u</string>
<string>https://a.passcloud.xyz/sz</string>
<string>-u</string>
<string>https://a.passcloud.xyz/hk</string>
<string>-u</string>
<string>tls://c.passcloud.xyz:853</string>
<string>-u</string>
<string>https://sm2.doh.pub/dns-query</string>
<string>-u</string>
<string>223.5.5.5</string>
<string>-f</string>
<string>119.29.29.29</string>
<string>-f</string>
<string>223.5.5.5</string>
<string>--all-servers</string>
<string>--fastest-addr</string>
<string>--cache</string>
<string>--cache-size=4096</string>
<string>--cache-optimistic</string>
<string>-b</string>
<string>119.29.29.29</string>
<string>-b</string>
<string>223.5.5.5</string>
</array>
<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/tmp</string>
</dict>
</plist>