我目前正在尝试编写一个脚本,以便在给定窗口ID的情况下定期获取特定窗口(活动或非活动)的屏幕截图。这是我的剧本:
#!/bin/bash
# captureScreen.sh command window_title image_file
DATE=`date -u +%Y-%m-%d-%H:%M:%S-UTC`
STATDIR=/cvs/cds/project/statScreen
today=$(date +"%Y%m%d")
hour=$(date +"%H")
DISPLAY=:5 #Virtual display name
export DISPLAY
#Parse arguments
CMD=$1
windowName=$2
fileName=${STATDIR}/images/$today/$3
archivedFile=${STATDIR}/images/$today/archive/$hour/$3
#Check if Xvfb server is already running
pid=`ps -eaf|grep vfb | grep ':5 -screen' | awk '{print $2}'`
if [ $pid ]; then
echo "Xvfb already running [pid=${pid}]" >/dev/null
echo "Xvfb already running [pid=${pid}]"
else
# Start Xvfb
echo "Starting Xvfb on $DISPLAY"
Xvfb $DISPLAY -screen 0 1600x1200x32 >&/dev/null &
fi
echo $pid
pid=$!
echo $pid
echo $pid > ${STATDIR}/Xvfb.pid
sleep 3
# Run the user command
#$CMD & #>&/dev/null &
${CMD} &> /dev/null &
pid=$!
echo $pid
# Testing
ps -eaf | grep $pid
echo $pid
# Wait for the window to come up (extended to 9 secs to avoid MEDM welcome window)
sleep 11
#Get the window id of the command
winID=`xwininfo -root -tree | grep -i $windowName | sed -n '1,1p' |awk '{print $1}'`
#winID=0x20011f
echo $pid
echo $winID
#Get the window id of the error message window
errID=`xwininfo -root -tree | grep -i errorMsgS | sed -n '1,1p' |awk '{print $1}'`
echo $winID
#echo $errID
if [ $errID ]; then
echo "Killing error message"
xkill -id $errID
fi
echo $pid
#echo $winID
#Get a screen shot
echo $fileName
echo $archivedFile
echo $winID
echo $PATH
echo $SHELL
import -delay 2 -window $winID $fileName
import -delay 2 -window $winID $archivedFile
#Kill the process
kill $pid
我尝试捕获的某些窗口工作正常,而其他窗口则给出了以下错误消息:
import: unable to read X window image `some_window_id': Resource temporarily unavailable @ error/xwindow.c/XImportImage/5027.
我记录输出时注意到的一点是,我尝试捕获的所有窗口的窗口ID都是相同的。我想这是因为我的脚本通过打开程序,截屏窗口,然后杀死程序来运行。如果需要更多信息,请与我们联系。任何帮助是极大的赞赏。如果可能的话,我想坚持使用imagemagick。