Cron 作业:如何运行需要打开显示的脚本? [英] Cron job: how to run a script that requires to open display?

查看:18
本文介绍了Cron 作业:如何运行需要打开显示的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个 cron 作业来运行 python 脚本,但它给了我这个错误:

I want to set up a cron job to run a python script, but it gives me this error:

RuntimeError: could not open display

这是因为我导入了一个需要我打开显示的模块(例如,pylab).即使我的脚本没有生成任何图片显示在监视器上.

This is because I import a module that requires me to open display (pylab, for example). Even though my script does not generates any pictures to display on the monitor.

有什么方法可以让 crontab 在显示打开的情况下运行我的作业(就像我 ssh -X 进入机器一样)?我实际上不需要为监视器生成任何图形.我只需要正确导入我的模块.

Is there any way to let crontab run my jobs with display open (just as if I ssh -X into a machine)? I don't actually need to generate any graphs to the monitor. I just need to import my modules correctly.

推荐答案

您需要有效的 DISPLAY 和 XAUTHORITY 才能在 Cron 中使用 X-Programms!

You will need a valid DISPLAY and XAUTHORITY to use X-Programms in Cron!

设置 DISPLAY 非常简单,在 Bash 中输入:

To set DISPLAY is very easy, type in the Bash:

export DISPLAY=":0.0"

为了获得有效的 XAUTHORITY,您必须寻找它.在 Debian/Gnome/gdm3 下,它们保存在 var/run/gdm3/*/database我使用了以下脚本:

In order to get a valid XAUTHORITY you have to look for it. Under Debian/Gnome/gdm3 they are saved in var/run/gdm3/*/database I used the following script:

export DISPLAY=":0.0"
[ -z $USER ] && USER=$( who | awk '{ print $1 }' | sort | uniq >/tmp/test )
for I in /var/run/gdm3/*; do
    AUTHUSER="`echo $I | awk -F '-' '{ print $3 }'`"
    for J in $USER; do
        [ "${AUTHUSER}" = "${J}" ] || continue
        USER="$J"
        export XAUTHORITY="${I}/database" && break
    done
done
sudo -u ${USER} /Path/to/xProgramm

Var $USER 可以为空,否则脚本会查找有效用户,否则您可以将 var 告诉脚本!

The Var $USER can be empty, than the script looks for a valid user, otherwise you can tell the script the var!

这篇关于Cron 作业:如何运行需要打开显示的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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