我编写了一个 macOS Mojave LaunchDaemon,它会触发 shell 脚本来运行图像快照 https://github.com/rharder/imagesnap程序。目的是每秒从相机捕获一张静态图像。
问题:守护进程失败并出现以下错误。我不知道是什么原因导致它失败,但看起来可能是相机不可用或 root/launchd 没有相机权限。请注意,脚本、自动化应用程序和在命令行上运行的 imagesnap 都可以自行工作,只有当我尝试从 LaunchDaemon 运行它们时才会失败。
我发现了什么:
- 该脚本作为普通用户运行良好,作为普通用户使用
sudo
,并使用 root 用户登录sudo su
。我在 iTerm2 中运行它并收到一个弹出窗口,请求访问相机,我批准了。该脚本在由 LaunchDaemon 运行时失败。
- 我创建了一个 Automator .app 并使用运行它
/usr/bin/open
。我再次收到一个弹出窗口,请求我批准相机许可。由 LaunchDaemon 运行时,automator .app 失败。
- 如果我省略第 4 行中的 imagesnap 命令,LaunchDaemon 运行脚本时不会出现错误。
- .plist 被加载并启动,并连续执行 shell 脚本,就像使用 KeepAlive 键集一样。
snapscript_err.log 片段
2019-02-20 15:54:06.183 imagesnap[34024:2892654] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] Inactive/invalid connection passed'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff33eb0ded __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff5ff7c720 objc_exception_throw + 48
2 AVFoundation 0x00007fff2fe56373 -[AVCaptureStillImageOutput _receiveSampleBuffer:] + 0
3 imagesnap 0x000000010590dfcf imagesnap + 12239
4 imagesnap 0x000000010590d618 imagesnap + 9752
5 imagesnap 0x000000010590cbab imagesnap + 7083
6 imagesnap 0x000000010590c6ca imagesnap + 5834
7 libdyld.dylib 0x00007fff6104aed9 start + 1
8 ??? 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException
/usr/local/bin/snapscript.sh: line 4: 34024 Abort trap: 6 /usr/local/bin/imagesnap -t 1
系统日志片段
Feb 20 15:57:58 macbook com.apple.xpc.launchd[1] (com.example.snapscript[34116]): Service exited with abnormal code: 134
Feb 20 15:57:58 macbook com.apple.xpc.launchd[1] (com.example.snapscript): Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
Feb 20 15:58:09 macbook com.apple.xpc.launchd[1] (com.example.snapscript[34121]): Service exited with abnormal code: 134
Feb 20 15:58:09 macbook com.apple.xpc.launchd[1] (com.example.snapscript): Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
Feb 20 15:58:19 macbook com.apple.xpc.launchd[1] (com.example.snapscript[34126]): Service exited with abnormal code: 134
Feb 20 15:58:19 macbook com.apple.xpc.launchd[1] (com.example.snapscript): Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
/Library/LaunchDaemon/com.example.snapscript.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>RunAtLoad</key>
<true/>
<key>Label</key>
<string>com.example.snapscript</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/snapscript.sh</string>
</array>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/var/log/snapscript/snapscript.log</string>
<key>StandardErrorPath</key>
<string>/var/log/snapscript/snapscript_err.log</string>
</dict>
</plist>
/usr/local/bin/snapscript.sh 文件
#!/bin/bash
cd /var/snapscript
/usr/local/bin/imagesnap -t 1