我正在使用“ 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
它工作正常。
请需要帮助。