如果需要 root,并且是需要用户登陆后才能运行,把 plist 放在 /Library/LaunchAgents/下
如果需要 root,并且不需要用户登陆后都能运行,把 plist 放在 /Library/LaunchDaemons/下
如果是需要用户身份,就在~/Library/LaunchAgents/下。
权限:
~/Library/LaunchAgents //特定用户登录后以当前用户启动,第三方程序一般都放这里
/Library/LaunchAgents //任一用户登录后以当前用户启动,管理员使用
/System/Library/LaunchAgents //系统组件,任一用户登录后以当前用户启动
/Library/LaunchDaemons //系统装载时以root用户启动,管理员使用
/System/Library/LaunchDaemons //系统组件,系统装载时以root用户启动
目录区别:
/System/Library //存放Apple开发的系统组件。
/Library //存放系统管理员安装的程序。
~/Library //存放普通用户安装的程序。
实例 权限 644
<?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>WorkingDirectory</key>
<string>/Users/xhw/Downloads/frp_0.28.2_darwin_amd64</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Users/xhw/Downloads/frp_0.28.2_darwin_amd64/frpc</string>
<string>-c</string>
<string>/Users/xhw/Downloads/frp_0.28.2_darwin_amd64/frpc.ini</string>
</array>
<key>StandardOutPath</key>
<string>/Users/xhw/Downloads/frp_0.28.2_darwin_amd64/logs.txt</string>
<key>StandardErrorPath</key>
<string>/Users/xhw/Downloads/frp_0.28.2_darwin_amd64/errors.txt</string>
<key>Label</key>
<string>kaijiqidong</string>
</dict>
</plist>
(注:launchctl load 非必须执行, 加上它是为了不重启mac的情况执行开机启动单项程序)
1.显示当前的启动脚本
launchctl list
2.开机时自动启动Apache服务器
launchctl load /System/Library/LaunchDaemons/org.apache.httpd.plist
launchctl start kaijiqidong
launchctl unload ~/Library/LaunchAgents/example.plist
launchctl list
plist中主要的字段和它的含义
Label 用来在launchd中的一个唯一标识,类似于每一个程序都有一个identifies一样。
UserName 指定运行启动项的用户,只有当Launchd 作为 root 用户运行时,此项才适用。
GroupName 指定运行启动项的组,只有当Launchd 作为 root 用户运行时,此项才适用。
KeepAlive 这个key值是用来控制可执行文件是持续运行呢,还是满足具体条件之后再启动。默认值为false,也就是说满足具体条件之后才启动。当设置值为ture时,表明无条件的开启可执行文件,并使之保持在整个系统运行周期内。
RunAtLoad 标识launchd在加载完该项服务之后立即启动路径指定的可执行文件。默认值为false。
Program 这个值用来指定进程的可执行文件的路径。
ProgramArguments 如果未指定Program时就必须指定该项,包括可执行文件文件和运行的参数。