2

Background

I have a script that changes the wallpaper on my Ubuntu 22 every 5 minutes:

#!/bin/bash

targetDir="/home/pedro/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


My objective is to run this script at startup and to that effect I have created a .dektop file at ~/.config/autostart/background_changer.desktop:

[Desktop Entry]
Version=1.0
Name=Wallpaper Changer
Comment=Periodically changes the background wallpaper, taking random images from the Pictures/wallpapers folder.
Exec=/home/mypc/background_changer.sh
Icon=/usr/share/icons/gnome-logo-text.svg
Terminal=false
Type=Application
Categories=Utility;

Permissions of this file:

$ stat background_changer.desktop 
  File: background_changer.desktop
  Size: 296             Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d   Inode: 5918548     Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1000/   mypc)   Gid: ( 1000/   mypc)
Access: 2023-01-16 09:55:14.680162348 +0100
Modify: 2023-01-11 15:38:45.036172742 +0100
Change: 2023-01-13 13:09:00.130731451 +0100
Birth: 2023-01-11 15:38:45.036172742 +0100

Problem

However, when I start my computer, nothing happens. Is there a way to verify that the script was started successfully?

Please note that using systemd is not possible due to the reasons mentioned in this post:

https://askubuntu.com/a/1449904/149504

Flame_Phoenix
  • 981
  • 4
  • 13
  • 31
  • Since your script runs in a endless loop, you should be able to locate the process (by its script name) in the output of the `ps -ef` command. – FedKad Jan 12 '23 at 09:16
  • Your script misses a shebang. Give your `.desktop`-file (not `.dektop`) a meaningful name like `wallpaperchanger.desktop`. Is the `.desktop`-file executable? Does your script work in a terminal? Check spelling, is it `Wallpapers` or `wallpapers`... – mook765 Jan 12 '23 at 09:41
  • @mook765 Indeed, what you said were the issues. After applying your suggestions, the file now works ! Please create an answer with that so I can accept it :D – Flame_Phoenix Jan 16 '23 at 09:03

1 Answers1

1

You can test a .desktop-file by executing it via double-click. If the .desktop-file does not work you have to troubleshoot a bit. First you'd check if the script works as expected in a terminal.

The script needs a shebang, #!/bin/bash as first line. Check the syntax of your script, ShellCheck can be very helpful. Or install the package shellcheck (sudo apt install shellcheck).

A .desktop-file has to be executable, so you may need to run

chmod +x ~/.config/autostart/background_changer.desktop

or use your filemanager to make the file executable.

Do yourself the favour and give the .desktop-file a meaningful name, so that you know the purpose of the file without looking at it's content. I'm not sure if the filename .desktop is a problem or not (at least it is a hidden file!), but there may be more .desktop-files in that directory (if not now then probably later). A meaningful name helps you to keep overview.

mook765
  • 14,911
  • 5
  • 35
  • 67