You are not logged in.

#1 2021-11-12 19:29:31

CaeriTech
Member
Registered: 2021-07-12
Posts: 31

systemd service to periodically set sway background

I select wallpapers is a couple of ways.  Some are downloaded, some are randomly selected from existing files and some are unique to particular scripts.  The selected wallpaper image is copied to a fixed location with a fixed name.

When I used Openbox I created a system service that would monitor the fixed location for a file change and set the wallpaper with nitrogen.

My conversion to Wayland is complete except for the setting of the background.

Nitrogen does not work for Wayland so I use swaybg but swaybg does not exit after setting a background like Nitrogen and must be kept running for the background to remain on the desktop. swaybg also cannot be run as root so I created a script to kill the running swaybg process after I set the new background and converted the system service to a user service.

The user service auto-start survives a re-boot if I execute:

loginctl enable-linger [username]

If I run the script executed by the service from a terminal the background is set as expected and stays set until I run it again.  However, when the script is executed by the service the background is set as expected but swaybg seems to die as soon as the service exits and the background is no longer on the desktop.

What changes or additions do I need to make to the service and/or script so that the new running swaybg process not die and waits for the service to kill it after I've chosen a new wallpaper?

Here is the service and script:

/etc/systemd/user/wallpaper.path

[Unit]
Description=wallpaper 

[Path]
PathChanged=~/wallpaper.jpg
Unit=wallpaper.service

[Install]
WantedBy=default.target

/etc/systemd/user/wallpaper.service

[Unit]
Description=wallpaper
ConditionFileIsExecutable=~/wallpaper.sh

[Service]
Type=oneshot
ExecStart=~/wallpaper.sh

~/wallpaper.sh

PID=$(pidof swaybg)
swaybg --image ~/wallpaper.jpg --mode fill &
sleep 1 ; [[ ${PID} != "" ]] && kill $PID

-=[ LIVE enabled UEFI with redundant syslinux pure systemd detached LUKS header partitionless encrypted GPT SSDx3 RAID0 wayland only because I can.  ]=-
    Backward compatibility is for the masses. There's no dual-boot here.

    [CaeriTech remains only artificially intelligent.  Turing would be aghast at just how artificial.]

Offline

#2 2021-11-13 00:29:59

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: systemd service to periodically set sway background

The swaybg process is part of the service, so it gets killed when the main process exits. You can change the type to forking so the service stays active, but you want it to stop so it can get triggered again by the path unit (which doesn't seem to have an option to trigger a restart of an active unit). You could set KillMode=process, but that is not recommended.

A cleaner solution would be to replace the script with a simple service that runs swaybg (and which you may want to hook up to default.target as well). Remove the condition from wallpaper.service and change to

ExecStart=systemctl --user restart swaybg.service

I fail to see the point of the enable-linger -- except if wallpaper.jpg is updated by a login script and swaybg only ever started by that service, then it could work as a "fix" for a race condition, but simply autostarting swaybg.service should also cover that without further changes to your user session.

Offline

#3 2021-11-13 19:27:13

froli
Member
From: Germany
Registered: 2008-06-17
Posts: 455

Re: systemd service to periodically set sway background

Maybe you have more luck using azote, a swaybg frontend.


archlinux on Macbook Pro 10,1

Offline

Board footer

Powered by FluxBB