问题描述
我在 PKG 安装程序中运行的安装后 Bash 脚本文件中有这一行:
launchctl load /Library/launchdaemons/com.mycompany.myapp.plist
PKG 正确安装了作业 plist 文件和作业调用的小程序。
但是当我检查作业是否在终端中加载时,我没有得到任何返回:
launchctl list | grep mycompany
如果我在终端中执行相同的加载命令,作业会按预期加载。
为什么脚本运行时没有加载作业?
解决方法
当您以普通用户身份运行 launchctl list
时,它会列出在您的用户会话中运行的 Launch Agents;要列出 Launch Daemons,请以 root 身份运行(即 sudo launchctl list
)。
更多详细信息:某些 launchctl
子命令允许您明确指定要处理的域。例如:
launchctl print system # Prints Launch *Daemons*
launchctl print user/501 # Prints Launch *Agents* for user #501's session
但较旧的“遗留”子命令,如 list
、load
、unload
、start
、stop
等早于该约定并使用用户命令运行的 ID 以确定要操作的域。例如:
launchctl load /path/to/plist # Loads the plist as an Agent in my user session
launchctl list # Lists Agents in my user session
sudo launchctl load /path/to/plist # Loads it as a Daemon in the system domain
sudo launchctl list # Lists Daemons in the system domain
您的包可能以 root 身份运行其脚本,因此它将作为守护程序加载作业(这正是您想要的),但它可能取决于包的确切配置方式。