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):


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


#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?

migrated from serverfault.com Feb 17 '11 at 18:45

This question came from our site for system and network administrators.

  • Yes, the invocation from cron is most likely the issue here. – Sven Feb 16 '11 at 12:28
  • Any idea what is the workaround to this? I need to take screenshots every 5 minutes – bakytn Feb 16 '11 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 '11 at 12:34
  • But it works if I invoke by hand. However I tested as you said and nothing... – bakytn Feb 16 '11 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. – Dennis Williamson Feb 16 '11 at 14:31

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.

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.