Cronjobs - Wallpaper Changer + examples

I want to start getting into Cronjobing things.

Does anyone have a good link to a list of useful common Cronjobs. I see alot of tuts on the tube to set one up.

Here’s my systemd setup for random background changes on Gnome 3. May it inspire you!
Note: this isn’t a cron job. It’s a systemd timer. Same kind of thing though.

Note on the following: Look at the list variable. I use a two folder deep hierarchy on my Wallpapers directory. You’ll have to modify it for whatever you use.

$ cat ~/.local/bin/randbg
#!/bin/bash

list=(~/Pictures/Wallpapers/*/*)
index=$((RANDOM % ${#list[@]}))
image="${list[$index]}"
verbose=0

x="$1"
while shift; do
        case "$x" in
                -v) verbose=1;;
        esac
        x="$1"
done

gsettings set org.gnome.desktop.background picture-options zoom
gsettings set org.gnome.desktop.background picture-uri "$image"

gsettings set org.gnome.desktop.screensaver picture-options zoom
gsettings set org.gnome.desktop.screensaver picture-uri "$image"

[ $verbose = 1 ] && echo -e "$image\t$index/${#list[@]}"
exit 0

Note: Looks like I had to use a full path to the program here which includes my home directory. Remember to replace it with your own.

$ cat ~/.config/systemd/user/randbg.service
[Unit]
Description=change background randomly

[Service]
ExecStart=/home/zlynx/.local/bin/randbg
Type=oneshot
$ cat ~/.config/systemd/user/randbg.timer
[Timer]
OnBootSec=1
OnUnitActiveSec=5m

[Install]
WantedBy=timers.target

And don’t forget:

$ chmod +x ~/.local/bin/randbg
$ systemctl --user enable --now randbg.timer
4 Likes

I don’t have any profound uses for cron, just running a command I already would run, automatically at the set time. Things like backup scripts, btrfs scrub/defrag/balance, etc.

This site is awesome for making sure you have the expression setup correctly so it will run when you want it too.
https://crontab.guru/

Also, if you are emailing the output, which is important if you are using cron for things like backups, I would suggest using cronic. It makes commands quiet, unless it exits with a non-zero, or outputs stderr, which makes cron not email you unless something has gone wrong, which is better then emailing you every single time, or never emailing you.

1 Like

Thanks @zlynx!!! I will start tinkering in my VM.

I’m Gnome-ubuntu 18.04

Ya rammed that in with no success
none of the config and systemd folders, and files were there. So i created them all in my user homefolder and nothing happened.

Does that randbg script run on its own and change the background? You need some wallpaper images in the directory you use to build the list variable.

Open and terminal and run randbg and fix it until it works.

i run randbg in terminal and nothing happens. The photos are in the folder. Tried fixing it.

In the randbg.timer & randbg.service where you have /user/ is it actually user or your user account name?

So there’s some script debugging tricks. You can run bash -x ~/.local/bin/randbg and it will output every command it is running.

You can also put an echo in there wherever. Like, if you want to see what it is getting for the list array you can do this:

echo list=${list[@]}

And for debugging you want to keep for later you can use that verbose variable, like I did for a couple of things.

1 Like

That ~/.config/systemd/user/ is actually /user/. It’s a systemd directory where you put user units, as opposed to system units. The ~/.config directory is like /etc but for individual users.

So global systemd units are in /usr/lib/systemd/system/ if they’re from packages, or /etc/systemd/system if they’re local sysadmin configuration. There are default, system-wide user units too, in /usr/lib/systemd/user

all i keep getting is:

bash: ~.local/bin/randbg: No such file or directory

`

`

Whoops, I missed a slash somehow. You need to make that ~/.local/bin/randbg. Sorry.

No such file or directory

Well sorry, I can’t debug that from here. Did you put the randbg file in there, or not?

Okay. Thanks i’ll keep looking at it

So I revised this fiddled with it got output of:

[email protected]:~$ bash -x ~/.local/bin/randbg echo

list=(~/home/aarasdvadsv/Documents/Wallpapers/)
index=0
image=/home/aarasdvadsv/home/aarasdvadsv/Documents/Wallpapers/
verbose=0
x=echo
shift
case “$x” in
x=
shift
gsettings set org.gnome.desktop.background picture-options zoom
gsettings set org.gnome.desktop.background picture-uri /home/aarasdvadsv/home/aarasdvadsv/Documents/Wallpapers/
gsettings set org.gnome.desktop.screensaver picture-options zoom
gsettings set org.gnome.desktop.screensaver picture-uri /home/aarasdvadsv/home/aaarasdvadsv/Documents/Wallpapers/
‘[’ 0 = 1 ‘]’
exit 0

fixed it the last one, now this,

alpha#[email protected]:~$ systemctl --user enable --now randbg.timer
Failed to enable unit: Unit file /home/alpha#sprite/.config/systemd/user/timers.target.wants/randbg.timer does not exist.

So it’s looking in the timer.target.wants folder but i don’t know why it’s going to that folder location? I got it going on 18.04 but 20.04 doesn’t like it.

It looks like you don’t have the randbg.timer file in .config/systemd/user/

yezzir

The window resizing glitched the whole gnome dash (VM) but i changed the gseting from zoom to stretch and its’ working seemless now.

But tried do it on the desktop and it didn’t work.

found my mistakes all sorted thanks @zlynx!!!

1 Like