背景
我创建了一个脚本,用于定期更改壁纸。当我从 bash 运行该脚本时,它运行得很好,但每次启动时都必须手动运行它,这很烦人,所以我决定使用 来自动执行该过程systemd
。
脚本
以下是更改壁纸的脚本:
targetDir="/home/mypc/Pictures/Wallpapers"
function get_next_photo() {
# Returns a random file form targetdir
files=( "$targetDir"/* )
echo "${files[RANDOM % ${#files[@]}]}"
}
function set_background() {
# Takes an absolute file path as argument. Need * for spaces in path
bg="$*"
echo "Setting background to $bg"
dconf write "/org/gnome/desktop/background/picture-uri" "'file://$bg'"
}
while :
do
background=$(get_next_photo)
echo "Next background is $background"
set_background $background
sleep 5m
done
这是systemd
我为其创建的服务,位于/etc/systemd/system/background-changer.service
:
[Unit]
Description=Periodically changes the background wallpaper, taking random images from the Pictures/wallpapers folder
[Service]
ExecStart=/bin/bash /home/mypc/background_changer.sh
[Install]
WantedBy=default.target
问题
问题是,一旦我开始使用systemd
壁纸,它就永远不会改变。使用检查后sudo systemd status background-changer.service
,我得到了以下结果:
$ sudo systemctl status background-changer.service
● background-changer.service - Periodically changes the background wallpaper, taking random images from the Pictures/wallpapers folder
Loaded: loaded (/etc/systemd/system/background-changer.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2023-01-09 13:17:32 CET; 7min ago
Main PID: 14196 (bash)
Tasks: 2 (limit: 18936)
Memory: 596.0K
CPU: 16ms
CGroup: /system.slice/background-changer.service
├─14196 /bin/bash /home/mypc/background_changer.sh
└─17655 sleep 5m
ene 09 13:17:32 systemd[1]: Started Periodically changes the background wallpaper, taking random images from the Pictures/wallpapers folder.
ene 09 13:17:32 bash[14196]: Next background is /home/mypc/Pictures/Wallpapers/jonatan-pie-3l3RwQdHRHg-unsplash.jpg
ene 09 13:17:32 bash[14196]: Setting background to /home/mypc/Pictures/Wallpapers/jonatan-pie-3l3RwQdHRHg-unsplash.jpg
ene 09 13:17:32 bash[14198]: error: Cannot autolaunch D-Bus without X11 $DISPLAY
ene 09 13:22:32 bash[14196]: Next background is /home/mypc/Pictures/Wallpapers/jonatan-pie-3l3RwQdHRHg-unsplash.jpg
ene 09 13:22:32 bash[14196]: Setting background to /home/mypc/Pictures/Wallpapers/jonatan-pie-3l3RwQdHRHg-unsplash.jpg
ene 09 13:22:32 bash[17652]: error: Cannot autolaunch D-Bus without X11 $DISPLAY
最后一行特别引起了我的注意:
error: Cannot autolaunch D-Bus without X11 $DISPLAY
我已经搜索了一段时间如何解决这个问题,但我找到的每一个解决方案都大不相同。我从重新安装图形驱动程序到使用密码管理器,中间还使用了其他东西,但都不起作用:
- https://anto.online/guides/cannot-autolaunch-d-bus-without-x11-display/
- 如果没有 X11 $DISPLAY,则无法自动启动 D-bus
- https://github.com/zowe/zowe-cli/issues/1139
- https://ubuntu-mate.community/t/error-cannot-autolaunch-d-bus-without-x11-display/11928
- 错误:没有 X11 $DISPLAY 无法自动启动 D-Bus
有人能帮助我理解为什么吗?
答案1
回答
使用 systemd 则无法做到这一点。基本上,它无法访问 DISPLAY 变量;变量从父进程传到子进程,而不是反过来。
实现这一目标的一种可能方法是使用文件.desktop
(~/.config/autostart
例如),但这超出了本问题的范围。
来源: