MacOS下Launch Daemon加载报错126:USB触发Shell脚本失败
Launch Daemon配置Error 126问题求助
我正尝试配置Launch Daemon,实现USB设备连接时自动运行Shell脚本。使用launchctl load com.myplist.plist注册守护进程时,出现Error 126错误。通过LaunchControl排查,仅提示问题出在Shell层面。该Shell脚本在终端中可正常运行,改用定时运行的plist配置时仍遇到相同错误。
USB触发的plist配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC -//Apple Computer//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.program</string> <key>ProgramArguments</key> <array> <string>/DIRECTORY TO FILE/testing.sh</string> </array> <key>LaunchEvents</key> <dict> <key>com.apple.iokit.matching</key> <dict> <key>com.apple.device-attach</key> <dict> <key>idProduct</key> <integer>1234</integer> <key>idVendor</key> <integer>1337</integer> <key>IOProviderClass</key> <string>IOUSBDevice</string> <key>IOMatchLaunchStream</key> <true/> </dict> </dict> </dict> </dict> </plist>
Shell脚本内容
#!/bin/bash # osascript -e 'display notification "" with title "RUNNING"' USB_FILE="/Volumes/Kindle/documents/My Clippings.txt" DEST_DIR="$HOME/Desktop" PYTHON_SCRIPT="PATH TO FILE/KindleObsidianV2.py" LOG_FILE="$HOME/Desktop/python_script.log" if diskutil list | grep -q "Kindle"; then if [ $? -eq 0 ]; then rsync -av "$USB_FILE" "$DEST_DIR" osascript -e 'display notification "File copied successfully" with title "USB File Copied"' python3 "$PYTHON_SCRIPT" # Check if Python script had an error if [ $? -ne 0 ]; then # Display error notification osascript -e 'display notification "Python script encountered an error. Check log file." with title "Python Script Error"' # Save Python log to desktop mv "$LOG_FILE" "$HOME/Desktop/python_script_error.log" else # Delete copied file rm "$DEST_DIR/My Clippings.txt" # Display completion notification osascript -e 'display notification "Script execution completed successfully" with title "Script Execution Completed"' fi else osascript -e 'display notification "Failed to copy file from USB. Check USB connection and try again." with title "Rsync Error"' fi else echo "USB device not found." fi
定时运行的plist配置(补充测试)
<?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.amer.kindle_monitor.plist</string> <key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>20</integer> <key>KeepAlive</key> <true/> <key>ProgramArguments</key> <array> <string>/Users/amer_/Desktop/KindleProject/testing.sh</string> </array> </dict> </plist>
已尝试的解决方案(均无效)
- 检查所有文件的运行权限
- 使用sudo和不使用sudo运行(使用sudo时因处于Launch Agents而非Daemons出现问题)
- 改用Launch Daemons(仍报相同错误)
- 在守护进程中显式配置终端的所有PATH变量
恳请提供帮助或建议!
内容的提问来源于stack exchange,提问作者Azal-Amer
相关产品推荐
相关产品推荐

