与cron的Gsettings [英] Gsettings with cron

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

问题描述

我写了一个bash脚本,改变墙纸(用于GNOME3)。

 #!/斌/庆典#壁纸的目录。
DIR =$ {HOME} /图片/壁纸/#随机壁纸。
壁纸=`找到$ {DIR}型F | SHUF -n1`#更改壁纸。
#http://bit.ly/HYEU9H
设置gsettings org.gnome.desktop.background画面选项跨越
gsettings设置org.gnome.desktop.background图片-URI的file:// $ {}壁纸

脚本在终端仿真程序执行(如GNOME终端)的伟大工程。在执行由cron或者ttyX终端得到的错误:

  **(工艺:26717):警告**:命令行`dbus-launch会--autolaunch = d64a75​​7758b286540cc0858400000603 --binary语法--close-标准错误退出与非零退出状态1:错误的AutoLaunch:X11初始化失败\\ n**(工艺:26717):警告**:命令行`dbus-launch会--autolaunch = d64a75​​7758b286540cc0858400000603 --binary语法--close-标准错误退出非零退出状态1:错误的AutoLaunch:X11初始化失败。 \\ n**(工艺:26721):警告**:命令行`dbus-launch会--autolaunch = d64a75​​7758b286540cc0858400000603 --binary语法--close-标准错误退出非零退出状态1:错误的AutoLaunch:X11初始化失败。 \\ n**(工艺:26721):警告**:命令行`dbus-launch会--autolaunch = d64a75​​7758b286540cc0858400000603 --binary语法--close-标准错误退出非零退出状态1:错误的AutoLaunch:X11初始化失败。 \\ n


解决方案

最后,我设法如何很多很多的尝试后仍未能解决这个问题。

事实上,出现问题的原因的cron只使用一个非常有限的组环境变量。而唯一一个环境变量,它是负责从当此设置为cron作业是 DBUS_SESSION_BUS_ADDRESS ,而不是显示或 XAUTHORITY GSETTINGS_BACKEND 或别的东西。这一事实也在指出还有这个答案

但在问题这个答案是的没有保证的是, DBUS_SESSION_BUS_ADDRESS 〜/ .dbus /会话总线/ 目录从当前GNOME会话更新为当前值的文件变量。走在这个问题的方法是找到一个过程在当前GNOME会话的PID,以及从环境中获得DBUS地址。我们可以做到这一点如下:

  PID = $(指派,由于gnome-session)#代替由于gnome-session'它也可以用来'noutilus'或'compiz的或图形化的进程的名称关于你确信你登录X会话后运行程序
出口DBUS_SESSION_BUS_ADDRESS = $(grep的-z DBUS_SESSION_BUS_ADDRESS的/ proc / $ PID / ENVIRON |切-d = -f2-)

话虽这么说,这个脚本应该是这样的:

 #!/斌/庆典#TODO:在只有黑夜壁纸。#壁纸的目录。
DIR =$ {HOME} /图片/壁纸/#出口DBUS_SESSION_BUS_ADDRESS环境变量
PID = $(指派,由于gnome-session)
出口DBUS_SESSION_BUS_ADDRESS = $(grep的-z DBUS_SESSION_BUS_ADDRESS的/ proc / $ PID / ENVIRON |切-d = -f2-)#随机壁纸。
壁纸=`找到$ {DIR}型F | SHUF -n1`#更改壁纸。
#http://bit.ly/HYEU9H
设置gsettings org.gnome.desktop.background画面选项跨越
gsettings设置org.gnome.desktop.background图片-URI的file:// $ {}壁纸

I wrote a bash script that changes the wallpaper (for GNOME3).

#!/bin/bash

# Wallpaper's directory.
dir="${HOME}/images/wallpapers/"

# Random wallpaper.
wallpaper=`find "${dir}" -type f | shuf -n1`

# Change wallpaper.
# http://bit.ly/HYEU9H
gsettings set org.gnome.desktop.background picture-options "spanned"
gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}"

Script executed in a terminal emulator (eg gnome-terminal) works great. During the execution by cron, or ttyX terminal getting the error:

** (process:26717): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n

** (process:26717): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n

** (process:26721): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n

** (process:26721): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n

解决方案

Finally I managed how to solve this issue after many, many attempts.

Indeed, the problem occur because cron uses only a very restricted set of environment variables. And the only one environment variable that is responsible for running in the right way the script from the question when this is set as a cron job is DBUS_SESSION_BUS_ADDRESS, not DISPLAY or XAUTHORITY or GSETTINGS_BACKEND or something else. This fact was also pointed well in this answer.

But the problem in this answer is that there's no guarantee that the DBUS_SESSION_BUS_ADDRESS variable from that file from ~/.dbus/session-bus/ directory is updated to the current value from the current gnome session. To go over this problem a method would be to find the PID of a process in the current gnome session, and obtain the dbus address from its environment. We can do this as follow:

PID=$(pgrep gnome-session)  # instead of 'gnome-session' it can be also used 'noutilus' or 'compiz' or the name of a process of a graphical program about that you are sure that is running after you log in the X session
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

That being said, the script should look like:

#!/bin/bash

# TODO: At night only dark wallpapers.

# Wallpaper's directory.
dir="${HOME}/images/wallpapers/"

# export DBUS_SESSION_BUS_ADDRESS environment variable
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

# Random wallpaper.
wallpaper=`find "${dir}" -type f | shuf -n1`

# Change wallpaper.
# http://bit.ly/HYEU9H
gsettings set org.gnome.desktop.background picture-options "spanned"
gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}"

这篇关于与cron的Gsettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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