Cron xdotool doesn't run
Asked Answered
A

6

6

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?

Anaclitic answered 15/7, 2016 at 8:56 Comment(2)
Do you need to pass any DISPLAY variable? Do you have anything in the logs?Mallorie
Yeah, i forgot place Display=: 0, thx youAnaclitic
M
9

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.

Mallorie answered 16/7, 2016 at 13:11 Comment(0)
D
2

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.

Disunity answered 29/12, 2018 at 23:23 Comment(0)
I
2

First as Parth said, make sure to type echo $DISPLAY on a terminal and get the output (for me it was :10.0 for some reason). Then inside the script that you are calling from cron, before you call xdotool, write the following:

export DISPLAY=:10.0 

#instead of :10.0 write your own output from before

export XAUTHORITY=/root/.Xauthority  

#if you are not a root user, instead of /root/.Xauthority write ~/.Xauthority
Instruction answered 30/1, 2020 at 7:42 Comment(0)
F
0

Why not

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

cd /usr/local/bin is not necessary here as you're not running the cron job in /usr/local/bin

You need cd in below scenario

*/1 * * * * cd /usr/local/bin && ls >/var/somelog # wish to execute ls in a particular directory
Fancy answered 15/7, 2016 at 9:4 Comment(1)
you have right, i have "test.log" in "/usr/local/bin" for output of refresh.shAnaclitic
C
0

Ubuntu 23.04

I wanted to run xdotool get_desktop and was getting Error: Authorization required, but no authorization protocol specified, Can't open display: (null)

Instead of just getting the DISPLAY, I also needed to get the XAUTHORITY (it was NOT /root/.Xauthority)

echo $DISPLAY
echo $XAUTHORITY

The fix was

export DISPLAY=<what you got from the $DISPLAY> && export XAUTHORITY=<what you got from @XAUTHORITY> && xdotool get_desktop

Example:

export DISPLAY=:0 && export XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.LYZR61 && xdotool get_desktop
Caridadcarie answered 28/6, 2023 at 8:46 Comment(0)
G
-1

If someone wants to schedule xdotool commands with different tool, It is possible with At command (see the tutorial). So, in order to work, you must define the environment variable DISPLAY (as explained by Kenorb). I first got the current value from that variable (echo $DISPLAY) in order to set in my script file (export DISPLAY=:0). Then, it can be ran like:

at xx:xx -f your_shell_script_file
Gittle answered 22/6, 2021 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.