与cron gsettings

Mat*_*łło 22 linux bash cron gnome

我写了一个改变壁纸的bash脚本(对于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}"
Run Code Online (Sandbox Code Playgroud)

在终端仿真器(例如gnome-terminal)中执行的脚本效果很好.在执行cron或ttyX终端时出错:

** (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
Run Code Online (Sandbox Code Playgroud)

Rad*_*anu 39

最后,我经过多次尝试后设法解决了这个问题.

实际上,问题的出现是因为cron只使用一组非常有限的环境变量.并且唯一一个环境变量负责以正确的方式运行来自问题的脚本,当它被设置为cron作业时DBUS_SESSION_BUS_ADDRESS,不是DISPLAYXAUTHORITYGSETTINGS_BACKEND其他.在这个答案中也指出了这个事实.

但是这个答案中的问题是,无法保证DBUS_SESSION_BUS_ADDRESS来自~/.dbus/session-bus/目录的该文件中的变量更新为当前gnome会话中的当前值.为了解决这个问题,一种方法是在当前gnome会话中找到进程的PID,并从其环境中获取dbus地址.我们可以这样做:

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-)
Run Code Online (Sandbox Code Playgroud)

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

#!/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}"
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢.我有一个脚本在午夜更新我的桌面壁纸.几个月前,此脚本停止正常工作.当我正常启动它时,它工作,但从crontab它没有做到这一点.有了这个技巧,我可以解决问题. (3认同)
  • 对于双屏显示器,将选项"spanned"更改为"zoom" (2认同)
  • 如果有多个用户登录怎么办?然后`pgrep` 将返回多个值 (2认同)
  • 在带有 X 的 Ubuntu 20.04 上,我必须使用 `PID=$(pgrep -t tty2 gnome-session)` (2认同)

Mat*_*łło 7

我找到了一些解决方案 当您导出文件〜/ .dbus/session-bus/*中包含的变量DBUS_SESSION_BUS_ADDRESS时,dbus-launch不会告诉您有关错误的更多信息.然而,而不是壁纸有人工制品.

新增代码:

sessionfile=`find "${HOME}/.dbus/session-bus/" -type f`
export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'`
Run Code Online (Sandbox Code Playgroud)

现在脚本看起来像这样:

#!/bin/bash

# TODO: At night only dark wallpapers.

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

# Weird, but necessary thing to run with cron.
sessionfile=`find "${HOME}/.dbus/session-bus/" -type f`
export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'`

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

# Change wallpaper.
# https://superuser.com/questions/298050/periodically-changing-wallpaper-under-gnome-3/298182#298182
gsettings set org.gnome.desktop.background picture-options "spanned"
gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}"
Run Code Online (Sandbox Code Playgroud)