Score:1

Fix error: Cannot autolaunch D-Bus without X11 $DISPLAY on script to change wallpaper using systemd

us flag

Background

I have created a script that changes my wallpaper periodically. The script works perfectly fine when I run it from bash, but having to manually run it on every startup is becoming annoying, so I decided I would automate the process using systemd.

Scripts

Following is the script that changes the wallpaper:

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

And this is the systemd service I created for it, located in /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

Problem

The problem is that once I start the job using systemd the wallpaper never changes. After checking with sudo systemd status background-changer.service, this is what I got:

$ 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

The last line specifically caught my attention:

error: Cannot autolaunch D-Bus without X11 $DISPLAY

I have searched for some time now how to fix this, but every solution I find is widely different. I went from reinstalling graphic drivers, to password managers with stuff in between, but nothing works:

Can someone help me understand why ?

in flag
Did you see [this answer](https://askubuntu.com/a/748046/1222991) and give it a try?
Flame_Phoenix avatar
us flag
Yes, I also tried that `xhost +` before executing my `systemctl start ...` but to no avail.
in flag
Cool. Perhaps it would be in everyone's best interest to have the "every solution I find is widely different" made a bit more specific so that there isn't a plethora comments that look essentially like mine but with different links
Flame_Phoenix avatar
us flag
Done! Post updated.
Score:0
us flag

Answer

This is not possible using systemd. Basically the DISPLAY variable will not be accessible to it; variables go from parent to child processes, not the other way around.

A possible way of making this work would be to use a use a .desktop file in ~/.config/autostart (for example) but this falls out of scope of this question.

Source:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.