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,不是DISPLAY或XAUTHORITY或GSETTINGS_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)
我找到了一些解决方案 当您导出文件〜/ .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)
| 归档时间: |
|
| 查看次数: |
10191 次 |
| 最近记录: |