Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've been searching all over the internet for an aswer to this but can't seem to find anything that works for me.

I want to run a cron job that automaticly takes a screenshot every minute. The script look like this:

#!/bin/bash
cd /home/ville/Skrivbord/screenshot
import -display :0 -win root screenshot.jpg

The code works fine when manually executed but does not work when i run it as a cron job.

The cron file look like this:

* * * * * /root/bin/screen.bash &> /dev/null
* * * * * /root/bin/syncdata.bash

The other task, syncdata works fine.

Here are some of the things i've tried without success:

Change permissions on /root/bin/screen.bash and /home/ville/Skrivbord/screenshot to 777 and change owner to root.

Change filename to screen.sh (maybe there's no difference between .bash and .sh?)

Change the import line to "import -win root screenshot.jpg" and "import -display :0.0 -win root screenshot.jpg".

Added the code:

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

One time above the existing code in screen.bash and one time below.

Changed the line in cron to "* * * * * export DISPLAY=:0 && /root/bin/screen.bash".

Nothing works!

Please help me

share|improve this question
    
There is absolutely no difference in what the file name ends with. All that matters is the #! line. –  grawity Aug 4 '11 at 22:06

1 Answer 1

up vote 2 down vote accepted

Programs started by the cron daemon do not have the authentication data needed to connect to your X server. Try putting something like this in your ~/.xprofile:

if [ "$XAUTHORITY" ]; then
    cp -f "$XAUTHORITY" ~/.Xauthority
fi
share|improve this answer
    
So i created a file /root/.xprofile and put the line there, but still no difference. Did i do it right? By adding 2> /home/ville/Skrivbord/output.txt to the cron tab i got this: No protocol specified import: unable to open X server `:0' @ error/import.c /ImportImageCommand/362. –  Vilhelm Frändberg Aug 4 '11 at 21:15
    
@Vilhelm: No, it's not right. The script should be in your home directory (unless you for some unimaginable reason run X11 as root). If the cronjob is being run as a different user, you will have to modify the xprofile script to copy "$XAUTHORITY" to a location accessible by that user, and then edit your cron job to export XAUTHORITY=<the new location> accordingly. –  grawity Aug 4 '11 at 22:08
    
@Vilhelm: Could you explain a bit more, in your original post, what exactly is all this supposed to achieve? (This seems too much like a "I want to know what my kids are browsing" kind of story.) –  grawity Aug 4 '11 at 22:12
    
No this is supposed to be on a computer which controls two instuments for atmospheric measurements. The data from the graphs and terminal etc should be displayed on the instruents homepage for public view and for the scientist to be able to quickly check the status. I will try to put xprofiles in my home directory. But i actually run vrontab under root, I will try to run it under user to se if that works better! (but i allready have a crontab under root that works fine...) –  Vilhelm Frändberg Aug 5 '11 at 9:01
    
Thank you so much grawity! I solved it by running crontab under user instead of root. Maybe the .xprofile file helped as well. Thank you! –  Vilhelm Frändberg Aug 5 '11 at 9:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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