Changing KDE Plasma desktop wallpaper from crontab not working?

I wanted to periodically change the desktop wallpaper with the command plasma-apply-wallpaperimage /path/to/img.png.

It works from a terminal in KDE and also from a TTY of the same user (although displaying a warning that it did not receive a reply).

But from the users crontab it does not work, even when I prepend the command with DISPLAY=:0.

Computer is used by one user only and user is logged in when cronjob is executed.

Any ideas what is the issue? Or suggestions how to get it to work? I thought this would be a simple thing…

Probably you need also add DBUS_SESSION_BUS_ADDRESS variable. Alternatively (and preferably) you can define systemd timer which should understand user context as described here: Using plasma-apply-wallpaperimage in cron job - #7 by Thra11 - Help - KDE Discuss

1 Like

What you’ve got here is an XY problem. You’re asking for advice on how to do something the way you think you should be doing it, while your idea of how to do it is already wrong to begin with.

Right-click your desktop and choose “Configure Desktop and Wallpaper”. Then change where the yellow arrow points in the screenshot below… :arrow_down:

… to “Slideshow”, add one or multiple folders to where the images are that you want to use for the slideshow, select which images you want to use — there’s a checkbox on every image — and set the timing interval. You can even select the order in which they must appear, or have them selected randomly.

2 Likes

I’m assuming that you are using a cron job instead of Plasma’s slideshow to change the wallpaper because the image changes regularly, but the name of the file doesn’t. Here are 2 suggestions:

Cron (I haven’t tested this):

Make sure you put the full path to the executable & file (do not use ~ instead of /home/user/) in the job:

/usr/bin/plasma-apply-wallpaperimage /full/path/to/img.png

That may get it running (as I said, I haven’t tested it)

A better solution would be to set up a systemd timer (and I have just tested this successfully):

Create these 2 files (make sure you don’t misspell the names) & place them in your ~/.config/systemd/user/ directory:

wallchange.service:

[Unit]
Description=A job to change the Plasma desktop wallpaper every 3 hours using a fixed-path file

[Service]
Type=simple

# Executable must be full path; the same with the image path. Do not use ~ as replacement for /home/user/
ExecStart=/usr/bin/plasma-apply-wallpaperimage /full/path/to/img.png

#[Install]
#WantedBy=default.target

wallchange.timer (edit as required):

[Unit]
Description=A job to change the Plasma desktop wallpaper every 3 hours using a fixed-path file
# Allow manual starts & stops
RefuseManualStart=no
RefuseManualStop=no

[Timer]
#Execute job if it missed a run due to machine being off
Persistent=false
#Run 15 minutes after login for the first time
OnStartupSec=15 min
#Run every 3 hours thereafter - comment out line below if using OnCalendar
OnUnitActiveSec=3h
#Run at 40 minutes past the hour at 3-hour intervals - comment out line below if using OnUnitActiveSec
#OnCalendar=00/3:40
#File describing job to execute
Unit=wallchange.service

[Install]
WantedBy=timers.target

Once you have those 2 files in your ~/.config/systemd/user/ directory run the following command as a regular user (don’t use sudo - it isn’t needed for your personal systemd jobs):

systemctl --user enable --now wallchange.timer

Your wallpaper should change immediately & continue to be changed according to the time interval you have set. The timer will start automatically 15 minutes after you login. You can check it is active via:

System Settings > Systemd > Timers

Also, whenever you make any changes to systemd .service or .timer scripts you should then run (without sudo) systemctl --user daemon-reload to send the changes through to systemd.

Useful resources:

Systemd Timers for Scheduling Tasks - Fedora Magazine

Systemd timers onCalendar (cron) format explained

Edit: added advice to reload the daemon when changes to timers/services are made

My 3 cents: As @Aragorn already mentioned; use the GUI – right-click the Desktop → choose “Configure Desktop and Wallpaper” (alt+d, alt+s).

I recall it being popular for about 5 minutes; 20 years ago; to use Cron for this purpose; there were seemingly endless forum posts on the topic. However, I don’t remember it working for many people; or surviving an upgrade, for that matter.

2 Likes

Thanks everyone for the good advice.

@Aragorn Good suggestion, I knew about the KDE slide-show option though, but it is not flexible/configurable enough for what I want to do. But I guess you’re still right that I was trying to do it the wrong way. To me it’s still a bit unintuitive that a script might behave differently when run from the users own crontab.

@scotty65 Big thank you for the detailed, complete and easy understandable solution. I’ve got it to work and was able to adapt it for my case with just minimal modifications :slight_smile:.

1 Like

I don’t see why. There’s nothing you can do with your cron job that you can also not do with the wallpaper slideshow in Plasma. :man_shrugging:

1 Like

@scotty65 Big thank you for the detailed, complete and easy understandable solution. I’ve got it to work and was able to adapt it for my case with just minimal modifications :slight_smile:.

I’m glad it worked out for you. I dove into systemd a few months ago and now I use it as not just a replacement for cron, but also instead of atd (the at daemon) for setting popup alarms & timers (my ADHD means I very easily forget that I have something cooking on the stove - until I smell smoke), and even as a replacement for the standard “shutdown -P” command (I set a shutdown time every day when I boot up to force me to go to bed at a decent time). I have some scripts that run when Plasma logs out, and the inbuilt shutdown command doesn’t log Plasma out the correct way to run those scripts. So I wrote something that does log out Plasma properly before shutting down. It does the job for me, just as the systemd wallpaper changer I suggested now does the job for you.

Also, another big advantage of systemd over cron for users is that it can launch application GUIs and send notifications (such as Plasma passivepopups or message boxes).

1 Like

Well with cron or systemd timers I can run a script which does give more options, e.g. I can choose a random wallpaper from a predefined source, at a certain time of day and make sure wallpapers don’t get reused.

I didn’t know about the systemd timers before, but it seems to be worth to look into it more. I do some of the things you mentioned too, just not with systemd so far.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.