logind (part of systemd) emits events when the system is locked, unlocked or
goes into sleep.
These events however, are simple D-Bus events, and don't actually run anything. There are no facilities for users to easily run anything on these events either (e.g.: a screen locker).
systemd-lock-handler is a small, lightweight helper fills this gap.
When the system is either locked, unlocked, or about to go into sleep, this
service will start the
systemd user targets respectively.
When the system is unlocked,
lock-target will be stopped.
Any service can be configured to start with any of these targets:
Note that systemd already has a
sleep.target, however, that's a system-level
target, and your user-level units can't rely on it. The one included in this
package does not conflict, but rather compliments that one.
A package is available in the AUR:
paru -S systemd-lock-handler
You can manually build and install:
firstname.lastname@example.org:~whynothugo/systemd-lock-handler cd systemd-lock-handler make build sudo make install
The service itself must be enabled for the current user:
systemctl --user enable --now systemd-lock-handler.service
Additionally, service files must be created and enabled for any service that should start when the system is locked.
enabling this service file would run
locks the session and before the system goes to sleep:
[Unit] Description=Screen locker for Wayland # If swaylock exits cleanly, unlock the session: OnSuccess=unlock.target # When lock.target is stopped, stops this too: PartOf=lock.target # Delay lock.target until this service is ready: After=lock.target [Service] # systemd will consider this service started when swaylock forks... Type=forking # ... and swaylock will fork only after it has locked the screen. ExecStart=/usr/bin/swaylock -f Restart=on-failure [Install] WantedBy=lock.target
PartOf=lock.target indicates to systemd that this service should
be stopped if
lock.target is stopped. This is even more important for
services that aren't the screen locker, since this setting means they'll get
stopped when the system is unlocked.
WantedBy=lock.target will have this service run when locking
or sleeping the system.
WantedBy=sleep.target will have this service run only when
sleeping the system. Note that the service will continue running after
waking up from sleep.
Lock your session using
This will mark the session as locked, and start
lock.target along with any
services that are
Unlock your session using
This will mark the session as unlocked, start
unlock.target, and stop
Service that are marked
PartOf=lock.target will be stopped when
Sleep your device using
This will start
sleep.target along with any services that are
This will happen before the system is suspended.
systemd-lock-handlerstarts. This ensure that there is enough time to react before the system actually goes to sleep. See this article for some background on how this. See also the updated example in the README to ensure that your screen locker has actually locked the screen before sleeping continues.
lock.targetitself. So for any services that should be started when either locking or suspending the system, specifying
Also handle sleep target.
systemd-lock-handler is licensed under the ISC licence. See LICENCE for details.