I am currently trying to write a script to periodically take a screenshot of specific windows, active or inactive, given the window ID. Here is my script:
#!/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
Some of the windows I am trying to capture are working fine, while others give me the following error message:
import: unable to read X window image `some_window_id': Resource temporarily unavailable @ error/xwindow.c/XImportImage/5027.
Something I've noticed from logging my output is that the window ID is the same for all of the windows I am trying to capture. I think this is because my script runs by opening the program, screenshotting the window, and then killing the program. Let me know if any more information is needed. Any help is greatly appreciated. I would like to stick to using imagemagick if possible.