如何获取macOS PKG安装脚本的Bash错误详情
问题背景
我开发的macOS应用PKG安装包包含preinstall.sh和postinstall.sh脚本,多数设备安装正常,但部分用户反馈脚本执行失败。用户提供的安装日志仅显示通用错误(如下),无法定位具体Bash报错:
Sep 14 15:04:16 Filestars-Mac-Mini installd[2540]: PackageKit: Install Failed: Error Domain=PKInstallErrorDomain Code=112 "An error occurred while running scripts from the package “Filestar.21.0.12.0.osx-x64.pkg”." UserInfo={NSFilePath=./preinstall, NSURL=file:///Users/filestar/repos/Filestar/Filestar.Installer/macOs/Output/Filestar.21.0.12.0.osx-x64.pkg#Filestar.pkg, PKInstallPackageIdentifier=com.Filestar.pkg.Filestar, NSLocalizedDescription=An error occurred while running scripts from the package “Filestar.21.0.12.0.osx-x64.pkg”.} {
NSFilePath = "./preinstall";
NSLocalizedDescription = "An error occurred while running scripts from the package “Filestar.21.0.12.0.osx-x64.pkg”.";
NSURL = "file:///Users/filestar/repos/Filestar/Filestar.Installer/macOs/Output/Filestar.21.0.12.0.osx-x64.pkg#Filestar.pkg";
PKInstallPackageIdentifier = "com.Filestar.pkg.Filestar";
}
解决方法
1. 修改脚本添加详细日志输出
直接在preinstall和postinstall脚本开头添加调试与日志重定向逻辑,捕获所有执行细节:
#!/bin/bash # 开启调试模式,记录每一步执行命令 set -x # 将标准输出和错误输出统一写入日志文件 exec > /tmp/Filestar_install.log 2>&1 # 原有脚本内容...
让用户重新安装后,提供/tmp/Filestar_install.log文件即可查看具体错误。
2. 指导用户查看系统控制台的完整安装日志
macOS的installd进程会输出更详细的脚本执行日志到系统控制台,用户可按以下步骤操作:
- 打开「控制台」应用(路径:
/Applications/Utilities/Console.app) - 在左侧导航栏选择「报告」>「安装」,找到对应时间的安装报告
- 或直接在搜索框输入
PKInstallErrorDomain或包标识符com.Filestar.pkg.Filestar,筛选出包含脚本具体错误的日志条目。
3. 让用户手动提取并执行脚本排查
如果用户能访问PKG文件,可指导他们解压脚本并手动运行:
- 复制PKG到桌面,执行解压命令:
pkgutil --expand ~/Desktop/Filestar.21.0.12.0.osx-x64.pkg ~/Desktop/Filestar_pkg
- 进入解压后的包目录,赋予脚本执行权限并运行:
cd ~/Desktop/Filestar_pkg/Filestar.pkg chmod +x preinstall ./preinstall
终端会直接显示脚本执行的错误信息。
4. 构建PKG时启用详细日志配置
若使用pkgbuild或productbuild构建安装包,可添加参数强制记录脚本日志:
- 使用
pkgbuild时,添加--install-logs指定日志路径:
pkgbuild --root ./your_app_files --identifier com.Filestar.pkg.Filestar --install-logs /tmp/Filestar_build_logs --scripts ./scripts Filestar.pkg
用户安装时,脚本的所有输出会被写入指定目录的日志文件。
内容的提问来源于stack exchange,提问作者Niels Bosma

