Xdotool using "DISPLAY=:0" not works in Crontab

I need to run shell script contains Xdotool codes in /home/z/Desktop/tempo/run.sh. I’ve tried many ways of DISPLAY=:0 but always not works.

I’ve tried each of below codes, and not works:

* * * * * export DISPLAY=:0 cd /home/z/Desktop/tempo; ./run.sh
* * * * * export DISPLAY=:0; cd /home/z/Desktop/tempo; ./run.sh
* * * * * export DISPLAY=:0 && cd /home/z/Desktop/tempo; ./run.sh
* * * * * DISPLAY=:0 cd /home/z/Desktop/tempo; ./run.sh
* * * * * DISPLAY=:0; cd /home/z/Desktop/tempo; ./run.sh
* * * * * DISPLAY=:0 && cd /home/z/Desktop/tempo; ./run.sh

Running directly also not works:

* * * * * export DISPLAY=:0 xdotool mousemove 20 20
* * * * * export DISPLAY=:0; xdotool mousemove 20 20
* * * * * export DISPLAY=:0 && xdotool mousemove 20 20
* * * * * DISPLAY=:0 xdotool mousemove 20 20
* * * * * DISPLAY=:0; xdotool mousemove 20 20
* * * * * DISPLAY=:0 && xdotool mousemove 20 20

I always don’t see my mouse moving on each line of codes above.

I’ve also tested Xdotool codes to make invalid website URI request and see the logs. Sadly the logs still blank.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

 * * * * * DISPLAY=:0 xdotool mousemove 20 20

At least should work as long as it’s in the crontab of the same user as the one having the X session on the corresponding display.

If another user is to do the mousemove, you need to grant him access to your display. This can be done by giving him the MIT Magick Cookie for your display and let him install it in his own X auth store (using xauth), or it can be done with:

xhost +si:localuser:the-user

Or it can be done by granting him access to your own X auth store, for instance, by doing:

setfacl -m u:the-user:r ~/.Xauthority

And change the crontab line to:

* * * * * DISPLAY=:0 XAUTHORITY=~me/.Xauthority xdotool...

If that other user is root you don’t need the setfacl step, but I would not run xdotool as root, no reason for that. You can run it as your own user.

Solution 2

cron jobs don’t even have a tty, let alone an X display.

If you want to test a GUI replaying inputs (or generating synthetic load), look for that. Don’t try to (ab)use cron.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply