crontab half working

I have set up a simple cronjob to run like this */5 * * * * /home/rodude123/wallpaper.sh
I basically want to change my wallpaper every 5 minutes for once it works I'll change it every hour. Every 5 minutes is just as a test.
wallpaper.sh downloads an image from unsplash from a random category and set's the wallpaper. For some reason, I have to change the wallpaper to a different one before changing back since the name is the same.

wallpaper.sh

#!/bin/bash

categories=()

while IFS= read -r line; do
    categories+=($line)
done < 'categories.txt'

randCat=$((RANDOM % ${#categories[@]}))
chosenCat=${categories[$randCat]}

wget  https://source.unsplash.com/1920x1080/?$chosenCat -O /home/rodude123/current.png

cp -f /home/rodude123/Pictures/wallpapers/current.png /home/rodude123/Pictures/wallpapers/old-current.png

mv -f /home/rodude123/current.png /home/rodude123//Pictures/wallpapers/current.png

# Doesn't run these 2 commands below at all.
dbus-send --session --dest=org.kde.plasmashell --type=method_call /PlasmaShell org.kde.PlasmaShell.evaluateScript 'string:
var Desktops = desktops();                                                                                                                       
for (i=0;i<Desktops.length;i++) {
        d = Desktops[i];
        d.wallpaperPlugin = "org.kde.image";
        d.currentConfigGroup = Array("Wallpaper",
                                    "org.kde.image",
                                    "General");
        d.writeConfig("Image", "file:///home/rodude123/Pictures/wallpapers/background.jpg");
}'

dbus-send --session --dest=org.kde.plasmashell --type=method_call /PlasmaShell org.kde.PlasmaShell.evaluateScript 'string:
var Desktops = desktops();                                                                                                                       
for (i=0;i<Desktops.length;i++) {
        d = Desktops[i];
        d.wallpaperPlugin = "org.kde.image";
        d.currentConfigGroup = Array("Wallpaper",
                                    "org.kde.image",
                                    "General");
        d.writeConfig("Image", "file:///home/rodude123/Pictures/wallpapers/current.png");
}'

So I have set everything up but the thing is it doesn't seem to want to change the wallpaper. It downloads it and does the moving and copying but doesn't change the wallpaper. If I manually run the script it works

Since it is running from a cron job, I would suspect that dbus-send doesn't have access to your session address. You may need to work around that.

Take a look through here to get some ideas:

Alternatively, you could try method #2 from this page instead of using dbus-send

or not use cron and systemctl --user ?

1 Like

@papajoke how else would I get it to run every hour let's say

A systemd timer using --user

https://wiki.archlinux.org/index.php/Systemd/timers

1 Like

@dalto I have tried to use a timer but it doesn't seem to work with dbus as it doesn't allow it

here is my wallpaper timer file

[Unit]
Description=Change desktop wallpaper

[Timer]
OnCalendar=hourly
Persistent=true

[Install]
WantedBy=wallpaper.target

here is my wallpaper service file

[Unit]
Description=Change desktop wallpaper

[Service]
Type=oneshot
ExecStart=/bin/bash /home/rodude123/wallpaperChanger/wallpaper.sh

[Install]
WantedBy=wallpaper.target

here is my wallpaper target file

[Unit]
Description=Change desktop wallpaper
StopWhenUnneded=yes

I have even tried to add this at the beginning of my bash file

export $(dbus-launch)

I get this error

Failed to connect to bus: No such file or directory

as writed top, use --user ! with user, service is load in "user" context
cp wallpaper.service and wallpaper.timer in ~/.config/systemd/user/

test service : systemctl start wallpaper --user
if ok, test timer : systemctl start wallpaper.timer --user
list timers: systemctl list-timers --all

Thanks for your help. I've been a bit stupid :sweat_smile: :smile:

Forum kindly sponsored by