用户 crontab +蟒蛇 +随机壁纸=不起作用?

发布于 2024-08-29 22:43:21 字数 875 浏览 4 评论 0原文

我有一个 python 脚本,可以通过 gconf 将桌面壁纸正确设置为给定文件夹中的随机图片。

然后,我的 crontab 中有以下条目

* * * * * python /home/bolster/bin/change-background.py

并且 syslog 正确报告执行情况

Apr 26 14:11:01 bolster-desktop CRON[9751]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:12:01 bolster-desktop CRON[9836]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:13:01 bolster-desktop CRON[9860]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:14:01 bolster-desktop CRON[9905]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:15:01 bolster-desktop CRON[9948]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:16:01 bolster-desktop CRON[9983]: (bolster) CMD (python /home/bolster/bin/change-background.py)

,但没有桌面变化,有什么想法吗?

I have a python script that correctly sets the desktop wallpaper via gconf to a random picture in a given folder.

I then have the following entry in my crontab

* * * * * python /home/bolster/bin/change-background.py

And syslog correctly reports execution

Apr 26 14:11:01 bolster-desktop CRON[9751]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:12:01 bolster-desktop CRON[9836]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:13:01 bolster-desktop CRON[9860]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:14:01 bolster-desktop CRON[9905]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:15:01 bolster-desktop CRON[9948]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:16:01 bolster-desktop CRON[9983]: (bolster) CMD (python /home/bolster/bin/change-background.py)

But no desktopy changey, Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好多鱼好多余 2024-09-05 22:43:21

您的脚本取决于 DISPLAY 环境变量,该变量在您从 X 会话中的 shell 执行脚本时设置,但在从 cron 运行脚本时取消设置。

Your script depends on the DISPLAY environment variable, which is set when you execute the script from the shell in an X session, but unset when the script is run from cron.

一个人的旅程 2024-09-05 22:43:21

要设置 DISPLAY 环境变量,我会将其直接放入 crontab 中。另外,我将使脚本可执行并为其提供适当的标头(#!/usr/bin/env python),以便可以直接执行。此外,您可以依赖在 crontab 运行时将 PWD 设置为 HOME。

我的 crontab 看起来像这样:

DISPLAY=:0.0
* * * * * bin/change-background.py

您还可以设置 PATH(以与 DISPLAY 相同的方式),这样就不需要 bin/ 了。


在 crontab 中设置环境的主要问题是值不是变量插值的。例如,这不会给出预期的结果:

PATH=$HOME/bin:$PATH

To set the DISPLAY environment variable, I would put it directly in the crontab. Also, I would make the script executable and give it a proper header (#!/usr/bin/env python) so that it can be executed directly. Additionally, you can rely on the PWD being set to HOME when the crontab runs.

My crontab would look like this:

DISPLAY=:0.0
* * * * * bin/change-background.py

You can also set the PATH (in the same manner as DISPLAY) so that the bin/ is not even needed.


The main gotcha for setting environment in the crontab is that values are not variable-interpolated. For example, this not give the expected results:

PATH=$HOME/bin:$PATH
谎言 2024-09-05 22:43:21

根据 Bolo 的观察,我忘记了将 DISPLAY 构建到脚本或 crontab 中。

最简单的解决方案是在 crontab 前面添加 env DISPLAY=:0.0

这样:

* * * * * env DISPLAY=:0.0 python /home/bolster/bin/change-background.py

As per Bolo's observation, I forgot about building in the DISPLAY into either the script or the crontab.

Easiest solution is to prepend the crontab with env DISPLAY=:0.0

so:

* * * * * env DISPLAY=:0.0 python /home/bolster/bin/change-background.py
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文