0

我正在使用“ imagesnap ”命令(通过 shell 脚本)在后台使用 launchd 从我的 OS X 应用程序中的网络摄像头捕获图像。

文件名:CaptureCameraPic

#!/bin/bash

timestamp=$(date +"%Y%m%d%H%M%S")

homeDirectory=$HOME/Documents

# Create Project Directory

if [ -d "$homeDirectory/MyApp" ]; then
echo "MyApp Directory exists"
cd $homeDirectory/MyApp
homeDirectory=$homeDirectory/MyApp

else
echo "MyApp Directory does not exists"

echo "Creating MyApp Directory"

cd $homeDirectory/
mkdir MyApp

echo "Created MyApp Directory successfully"

cd $homeDirectory/MyApp
homeDirectory=$homeDirectory/MyApp

fi

# Camera
if [ -d "$homeDirectory/Camera" ]; then
# Control will enter here if $DIRECTORY exists.
echo "Camera Directory exists"
cd $homeDirectory/Camera
camera_filename="IMG_${timestamp}.jpg"
echo "Take picture $camera_filename"

imagesnap "$camera_filename"

else
echo "Directory does not exists"
echo "Create directory"
cd $homeDirectory
mkdir Camera
cd $homeDirectory/Camera
camera_filename="IMG_${timestamp}.jpg"
echo "Take picture $camera_filename"

imagesnap "$camera_filename"

fi

我的 plist (test.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>GroupName</key>
    <string>user</string>
    <key>UserName</key>
    <string>root</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>Path_to_ CaptureCameraPic </string>
    </array>
    <key>OnDemand</key>
    <false/>
    <key>Nice</key>
    <integer>1</integer>
    <key>StartInterval</key>
    <integer>10</integer>
    <key>StandardErrorPath</key>
    <string>Path_to_Error_log.log</string>
    <key>StandardOutPath</key>
    <string>Path_to_Output_log.log</string>
</dict>
</plist>

我将脚本和 plist 文件都保存在应用程序包路径中。

在我的 os x 应用程序中,单击登录按钮时,我将 plist 复制到/Library/LaunchDaemons/

该脚本开始运行(使用launchctl load path_to_plist_in_LaunchDaemon_folder)(我可以在输出文件中看到日志打印,例如文件夹创建等)。

但是对于“imagesnap”,它会抛出错误

imagesnap: command not found

但是当我在终端上运行这个脚本时

chmod +x CaptureCameraPic
./CaptureCameraPic

它工作正常。

请需要帮助。

4

1 回答 1

0

正如@Rogier 建议的那样,您需要imagesnap脚本中的完整路径。您可以通过以下方式找到:

which imagesnap

或者

find /usr /opt -name imagesnap -type f -print

然后,您将在脚本中使用以下内容:

/path/to/imagesnap ...

此外,您的脚本中有很多乱七八糟的东西,您可以像这样更简单地做一些事情:

#!/bin/bash

# Set a variable just once so there is less maintenance
photodir="$HOME/Documents/MyApp/Camera"

# Make the directory if it doesn't exist, don't complain if it does
mkdir -p "$photodir"

cd "$photodir"
if [ $? -ne 0 ]; then
   echo "Failed to set working directory to $photodir"
   exit 1
fi
...
/path/to/imagesnap ...

此外,您的时间戳非常糟糕 - 想象一下必须稍后解析它!在其中放置一些分隔符-也许是这样:

date +"%Y-%m-%d_%H:%M:%S"
2017-01-27_10:24:35 
于 2017-01-27T10:25:03.130 回答