Configuring systemd to manage a service on boot
Solution 1:
You can't change background in a system boot script because your graphical environment isn't started yet. I'd suggest adding your script to your startup applications in gnome-tweaks.
There's actually two parts to this.
One is the gsettings change you are making -- which is actually permanent and doesn't need to be repeated unless something else is changing it and you want to reset it, or if you want to change the filename.
The other is downloading the image, and if you replace the file rather than using a new name, it may change instantly.
The download also likely fails at boot because the network may not be fully up when it runs, despite the network-online.target dependency. Fixing this would delay the system booting -- this would probably also be better done during login startup instead.
Solution 2:
I've used a similar system, so you should be able to get it working with systemd. You can include a systemd timer along with your service file to trigger the service to run at boot after you login. Since root priviledges aren't required for setting a wallpaper, you can do as @Jeff Schaller suggested and create a systemd user service. You can store your systemd user files in $HOME/.config/systemd/user
You can modify the apod.service file to something like:
[Unit]
Description=Set APOD as Desktop
After=network.target
After=systemd-user-sessions.service
After=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
ExecStart=/bin/bash /home/me/apod.sh
Add a file called apod.timer
[Unit]
Description=Timer for apod.service
[Timer]
OnBootSec=0 min
[Install]
WantedBy=timers.target
Place apod.service
and apod.timer
in $HOME/.config/systemd/user/
and enable them like normal, but instead of using root privileges/invoking sudo, do so as a normal user and add the --user
flag to the command:
systemctl --user enable apod.timer
systemctl --user enable apod.service
Edit: It also appears that the 'image' of the day for today's date from linked nasa source is a video, so that could be something to watch out for.
Edit2: left off enabling the .service
in addition to the .timer