4

我写了一个 macOS Mojave LaunchDaemon,它触发一个 shell 脚本来运行imagesnap程序。目的是每秒从相机中捕获一张静止图像。

问题:守护程序失败并出现以下错误。我不知道是什么导致它失败,但似乎相机不可用或 root/launchd 没有相机权限。请注意,脚本、自动化应用程序和在命令行上运行的 imagesnap 都可以自行运行,并且只有在我尝试从 LaunchDaemon 运行它们时才会失败。

我发现了什么:

  1. 该脚本作为普通用户、使用 的普通用户sudo以及使用 登录的 root 用户运行良好sudo su。我在 iTerm2 中运行它并弹出一个请求访问摄像头的弹出窗口,我同意了。该脚本在由 LaunchDaemon 运行时失败。
  2. 我创建了一个 Automator .app 并使用/usr/bin/open. 我再次收到一个请求我批准的相机许可的弹出窗口。由 LaunchDaemon 运行时,automator .app 会失败。
  3. 如果我在第 4 行省略了 imagesnap 命令,LaunchDaemon 运行脚本不会出错。
  4. .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

system.log 片段

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
4

0 回答 0