将相机权限授予macOS Mojave LaunchDaemon

时间:2019-02-20 07:22:45

标签: macos plist launchd macos-mojave launch-daemon

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

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

我发现的问题:

  1. 该脚本可以作为普通用户,使用sudo的普通用户以及使用sudo su登录的root用户正常运行。我在iTerm2中运行它,并弹出一个请求访问摄像头的弹出窗口,这是我批准的。由LaunchDaemon运行时,脚本失败。
  2. 我创建了一个Automator .app,并使用/usr/bin/open来运行它。再次出现一个弹出窗口,请求我批准的摄像头许可。由LaunchDaemon运行时,自动程序.app失败。
  3. 如果我在第4行中省略了imagesnap命令,则LaunchDaemon会运行脚本而不会出现错误。
  4. .plist将被加载并启动,并通过设置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

0 个答案:

没有答案