Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron xdotool doesn't run

I am new to using crontab, and I've been trying to get a simple cron job. I want press F5 every 1 minute to refresh Mozzila Firefox. I am using xdotool for press F5. I have script /usr/local/bin/refresh.sh:

#!/bin/bash  
xdotool search --name "Mozilla Firefox" key F5

If i run it in command line it works fine. And permission:

-rwxr-xr-x. 1 root root 89 15. čec 10.32 refresh.sh

In crontab i have:

*/1 * * * * cd /usr/local/bin && sh refresh.sh

But script run by cron doesnt work. Can anyone tell me what i do wrong?

like image 233
Tomas Avatar asked Jul 15 '16 08:07

Tomas


2 Answers

The xdotool command is automation tool for X11 which allows you simulate keyboard/mouse input, but since crontab is run independently, it's required to define DISPLAY variable to specify which X Window System display server to use. Normally when you're login to the desktop this variable is assigned automatically, but crontab is running jobs in isolated environment (doesn't have even a tty associated), especially when you run commands via root account.

So in short, you should do define your job like:

*/1 * * * * DISPLAY=:0 /usr/local/bin/refresh.sh

Or you can define variables at the beginning of the file (in case of Vixie cron). See: Variables in crontab?

Also make sure the user which is running the job has granted access to the selected X display. If you need to grant the access, you need to assign the permission via xhost and setfacl commands and specify extra XAUTHORITY variable, see: Xdotool using “DISPLAY=:0” for more details.

like image 90
kenorb Avatar answered Sep 21 '22 02:09

kenorb


So I tried a bunch of things, but for some reason on Ubuntu 18.04 echo $display returned :1 not :0. Also the only environment variable setter that seemed to work was adding:

export DISPLAY=":1"

directly to the script that cron is running.

like image 43
Parth Mehrotra Avatar answered Sep 20 '22 02:09

Parth Mehrotra