1

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 ?

Flame_Phoenix
  • 1,071
  • 5
  • 16
  • 32

1 Answers1

0

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:

Flame_Phoenix
  • 1,071
  • 5
  • 16
  • 32