Shell – Scheduled bash script using systemd timer

shell-scriptsystemdsystemd-timer

I wanted to change my wallpaper periodically and randomly from a folder with pictures.
I used the program "feh" and wrote a little script with bash called wallpaperscript.sh:

#!/bin/bash
files=(/torellino/Dropbox/Dropbox/Sfondi/*)
feh --bg-scale ${files[RANDOM % ${#files[@]}]}

When I use the script on a terminal it works and the wallpaper is changed randomly.
In order to automate the process and let the script execute every few minutes I decided to use systemd timer. So i created 2 files and placed them in /etc/systemd/system/:

wallpaper.service:

[Unit]
Description=Call wallpaperscript.sh and change the wallapaper

[Service]
ExecStart="/etc/systemd/system/wallpaperscript.sh"

And wallpaper.timer:

[Unit]
Description=Runs wallpaperscript every 10 minutes

[Timer]
# Time to wait after booting before we run first time
OnBootSec=1min
# Time between running each consecutive time
OnUnitActiveSec=10min
Unit=wallpaper.service

[Install]
WantedBy=multi-user.target

I actually did not create those myself but I found them somewhere. Anyway I enabled and started wallpaper.timer but it did not work. If I check "systemctl status wallpaper.timer" I can actally see that the timer is working:

● wallpaper.timer - Runs wallpaperscript every 10 minutes
Loaded: loaded (/etc/systemd/system/wallpaper.timer; enabled; vendor preset: disabled)
Active: active (waiting) since Fri 2018-06-01 19:42:40 CEST; 16min ago
Trigger: Fri 2018-06-01 20:03:47 CEST; 4min 14s left

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

wallpaper.service on the other side gives me an error:

● wallpaper.service - Call wallpaperscript.sh and change the wallapaper
Loaded: loaded (/etc/systemd/system/wallpaper.service; static; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2018-06-01 19:53:47 CEST; 7min ago
Process: 919 ExecStart=/etc/systemd/system/wallpaperscript.sh (code=exited, status=2)
Main PID: 919 (code=exited, status=2)

I searched for solutions but I did not find any. By the way it is the first time that I am using systemd so I really cannot figure out the problem myself.

I am using arch-linux with i3 if that can help anyhow

I also got the sderr for wallpaper.service and I got this:

"sudo journalctl -u wallpaper.service"

giu 01 19:43:38 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 19:43:39 placobravo wallpaperscript.sh[452]: feh WARNING: You have no HOME, cannot read configuration
giu 01 19:43:39 placobravo wallpaperscript.sh[452]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 19:43:39 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 19:43:39 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 19:53:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 19:53:47 placobravo wallpaperscript.sh[919]: feh WARNING: You have no HOME, cannot read configuration
giu 01 19:53:47 placobravo wallpaperscript.sh[919]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 19:53:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 19:53:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 20:04:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 20:04:47 placobravo wallpaperscript.sh[1108]: feh WARNING: You have no HOME, cannot read configuration
giu 01 20:04:47 placobravo wallpaperscript.sh[1108]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 20:04:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 20:04:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 20:15:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: feh WARNING: You have no HOME, cannot read configuration
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: feh WARNING: /torellino/Dropbox/Dropbox/Sfondi/the does not exist - skipping
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: feh WARNING: witcher3.jpg does not exist - skipping
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: feh: No loadable images specified.
giu 01 20:15:47 placobravo wallpaperscript.sh[1343]: See 'feh --help' or 'man feh' for detailed usage information
giu 01 20:15:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=1/FAILURE
giu 01 20:15:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 20:26:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 20:26:47 placobravo wallpaperscript.sh[1388]: feh WARNING: You have no HOME, cannot read configuration
giu 01 20:26:47 placobravo wallpaperscript.sh[1388]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 20:26:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 20:26:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.
giu 01 20:37:47 placobravo systemd[1]: Started Call wallpaperscript.sh and change the wallapaper.
giu 01 20:37:47 placobravo wallpaperscript.sh[1579]: feh WARNING: You have no HOME, cannot read configuration
giu 01 20:37:47 placobravo wallpaperscript.sh[1579]: feh ERROR: Can't open X display. It *is* running, yeah?
giu 01 20:37:47 placobravo systemd[1]: wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
giu 01 20:37:47 placobravo systemd[1]: wallpaper.service: Failed with result 'exit-code'.

From what I can understand it seems that feh cannot access X as Jeff Schaller pointed out.
Does anybody have any idea how I could solve this?

Best Answer

Welcome to StackExchange.

Your output says that feh can't open the X server. You should set a couple of values in wallpaper.server

User=

Set this the user you want to set the wallpaper for.

Environment=DISPLAY=:0

Needs to go in the Display section. This post explains why both are needed.

Related Question