Mac登录脚本执行失败:如何修复launchctl Bootstrap I/O错误?
macOS登录时自动运行Shell脚本失败:Bootstrap failed: 5: Input/output error
问题背景
我需要让一个Shell脚本在每次登录MacBook时自动运行一次(非持久化守护进程,执行写入操作后即结束,下次登录再触发),尝试通过launchd实现,但遇到了执行失败的问题。
配置文件与脚本内容
plist配置文件
我创建的plist文件已通过plutil -lint校验无语法问题,内容如下:
<?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>com.example.test</string> <key>Program</key> <string>/Users/Tom/Desktop/tomtest.sh</string> <key>KeepAlive</key> <false/> <key>OnDemand</key> <false/> <key>LaunchOnlyOnce</key> <false/> <key>StandardOutPath</key> <string>/Users/Tom/Desktop/tomtest.log</string> <key>StandardErrorPath</key> <string>/Users/Tom/Desktop/tomtest.log</string> <key>Debug</key> <true/> </dict> </plist>
Shell脚本内容
脚本tomtest.sh手动运行可正常写入日志,内容如下:
#!/bin/sh echo "I ran" > /Users/Tom/Desktop/tomtest.log exit 0
操作步骤与异常现象
- 将plist文件复制到
~/Library/LaunchAgents/com.example.test.plist,并通过sudo chown root:wheel设置权限,之后系统通知提示添加了登录项,在系统设置>通用>登录项与扩展中可看到该脚本 - 注销并重新登录后,日志文件未生成;手动创建日志文件后,也没有内容写入
- 使用
launchctl调试时持续报错:~ $ launchctl bootstrap gui/501 com.example.test Bootstrap failed: 5: Input/output error Try re-running the command as root for richer errors. ~ $ sudo launchctl bootstrap gui/501 com.example.test Password: Bootstrap failed: 5: Input/output error - 执行
launchctl print gui/501查看状态,仅显示旧Label名称com.example.OLDtest的异常条目,新Label的条目未出现;运行launchctl load ~/Library/LaunchAgents/com.example.test无报错,但日志仍为空
核心疑问
为何如此简单的程序会出现Bootstrap failed: 5: Input/output error错误?
补充说明
我的真实需求是让一个每日生成桌面壁纸的zsh脚本(手动运行正常)在登录时自动执行一次,无需打开终端。设备为M4 MacBook Air,系统版本为macOS Sequoia 15.5。
内容的提问来源于stack exchange,提问作者Tom




