用户 crontab +蟒蛇 +随机壁纸=不起作用?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的脚本取决于
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.要设置 DISPLAY 环境变量,我会将其直接放入 crontab 中。另外,我将使脚本可执行并为其提供适当的标头(
#!/usr/bin/env python
),以便可以直接执行。此外,您可以依赖在 crontab 运行时将 PWD 设置为 HOME。我的 crontab 看起来像这样:
您还可以设置 PATH(以与 DISPLAY 相同的方式),这样就不需要
bin/
了。在 crontab 中设置环境的主要问题是值不是变量插值的。例如,这不会给出预期的结果:
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:
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:
根据 Bolo 的观察,我忘记了将 DISPLAY 构建到脚本或 crontab 中。
最简单的解决方案是在 crontab 前面添加
env DISPLAY=:0.0
这样:
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: