Cron xdotool无法运行 [英] Cron xdotool doesn't run

查看:161
本文介绍了Cron xdotool无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用crontab并不陌生,并且我一直在尝试获得简单的cron作业。
我想每1分钟按F5刷新Mozzila Firefox。我正在按xdotool来按F5。
我有脚本 /usr/local/bin/refresh.sh:

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

crontab 中具有:

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

但是cron运行的脚本不起作用。
谁能告诉我我做错了什么吗?

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

推荐答案

xdotool 命令是X11的自动化工具,它使您可以模拟键盘/鼠标输入,但是由于crontab是独立运行的,因此需要定义 DISPLAY 变量以指定要使用的X Window System显示服务器。通常,当您登录到桌面时,该变量是自动分配的,但是crontab在隔离的环境中运行作业(甚至没有tty关联),尤其是当您通过 root 帐户。

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

或者您可以在文件的开头定义变量(对于Vixie cron)。请参阅: crontab中的变量?

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

还要确保用户正在运行的作业已授予对所选X显示的访问权限。如果需要授予访问权限,则需要通过 xhost setfacl 命令分配权限,并指定额外的 XAUTHORITY 变量,请参见:使用 DISPLAY =:0的Xdotool 有关更多详细信息。

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.

这篇关于Cron xdotool无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆