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?

6
  • Yes, the invocation from cron is most likely the issue here.
    – Sven
    Feb 16, 2011 at 12:28
  • Any idea what is the workaround to this? I need to take screenshots every 5 minutes
    – bakytn
    Feb 16, 2011 at 12:31
  • what happens if just for a test you set NM=test, just to ensure its not a problem with your date command.
    – pablo
    Feb 16, 2011 at 12:34
  • But it works if I invoke by hand. However I tested as you said and nothing...
    – bakytn
    Feb 16, 2011 at 12:40
  • 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. Feb 16, 2011 at 14:31

1 Answer 1

10

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.

2
  • Oh man you ROCk! This worked! You cool. P.S: Hey guys please vote up!
    – bakytn
    Feb 16, 2011 at 12:57
  • @bakytn, if this answer solved your problem you should accept it (click the checkbox to the left). Thanks!
    – larsks
    Feb 16, 2011 at 15:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .