ImageMagick's "import" command line program failing to take a screenshot from CRON script

0

Hello I wrote a simple command line script which is suppose to take my screenshot every 5 minutes.

(using ImageMagick's "import" program)

here is the script (shottr.sh):

#!/bin/sh

PTH="/home/username/images"
NM=`date +%j`_`date +"%F %k:%M"`

/usr/bin/import -window root -resize 1024 "$PTH/$NM.png" &
echo "Screenshot" | festival --tts &

this is working fine if I execute by hand i.e:

# ./shottr.sh

or

#sh shottr.sh

the cron is set like this:

*/5 * * * * /bin/sh /home/username/scripts/shottr.sh

the script itself is being executed (I hear a voice saying: "Screenshot") but the actual screenshot is not taken.

P.S: Be assured that it is NOT permission issue (I placed a simple "touch" invocation and file was created)

may be if it's run from cron...it doesn't have a "window" so it can't take a screenshot from nowhere?? If that is the case, then how can I workaround it?

bakytn

Posted 2011-02-16T12:23:15.593

Reputation: 1 537

Yes, the invocation from cron is most likely the issue here. – Sven – 2011-02-16T12:28:38.380

Any idea what is the workaround to this? I need to take screenshots every 5 minutes – None – 2011-02-16T12:31:00.530

what happens if just for a test you set NM=test, just to ensure its not a problem with your date command. – pablo – 2011-02-16T12:34:40.103

But it works if I invoke by hand. However I tested as you said and nothing... – None – 2011-02-16T12:40:10.487

Your date string can be simplified to: NM=$(date "+%j_%F %k:%M") (no need to run date twice). Putting the Julian day number first ruins the sortability of the filenames and you might want to consider not including a space. – Paused until further notice. – 2011-02-16T14:31:22.873

Thanks Dennis, for the tip. And you are right about the naming. Fixed. – None – 2011-02-16T15:58:34.063

Answers

8

When you're running the script from cron it doesn't have access to your DISPLAY environment variable, which is how it knows what X11 display with which to interact. You may be able to get it to work by adding the following to your script:

# Set display to :0 if it's not already set.
: ${DISPLAY:=:0}
export DISPLAY

This assumes that your DISPLAY is always :0, which is true if you're logging in on the console but not true if you're running X remotely.

larsks

Posted 2011-02-16T12:23:15.593

Reputation: 3 245

Oh man you ROCk! This worked! You cool.

P.S: Hey guys please vote up! – None – 2011-02-16T12:57:53.243

@bakytn, if this answer solved your problem you should accept it (click the checkbox to the left). Thanks! – larsks – 2011-02-16T15:14:16.303