Screenshot cron job not working

1

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

Vilhelm Frändberg

Posted 2011-08-04T16:51:55.807

Reputation: 13

There is absolutely no difference in what the file name ends with. All that matters is the #! line. – user1686 – 2011-08-04T22:06:41.680

Answers

2

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

user1686

Posted 2011-08-04T16:51:55.807

Reputation: 283 655

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  – 2011-08-04T21:15:17.200

@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. – user1686 – 2011-08-04T22:08:10.323

@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.) – user1686 – 2011-08-04T22:12:01.493

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 – 2011-08-05T09:01:46.160

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 – 2011-08-05T09:21:40.293