Locking Your Machine Before Suspending Using systemd

If you're running Linux with a Desktop Environment such as KDE or Gnome, you'll get things like automagic screen locking / screensavers out-of-the-box.

But as someone who runs a more lightweight Window Manger, I have to manage this myself.

In the past I've made sure that when triggering a suspend I first trigger my lockscreen, but that was a bit cumbersome, especially when I found that systemd could do this for me.

For instance, I have the file /etc/systemd/system/i3lock.service:

[Unit]
Description=Lock the screen with i3lock
# Ensure that we run this service before the machine can actually go to sleep
Before=sleep.target

[Service]
User=jamie
# Because the i3lock process blocks while it's running, this needs to be
# `forking`, otherwise systemd would never supsend while `i3lock` is running
Type=forking
# There's usually only one interactive Xorg session running, so it's almost
# certainly going to be the 0th display
Environment=DISPLAY=:0
# /home/jamie/.currbg is the location of whatever my current background photo
# is, as I like having an image background for i3lock
ExecStart=/usr/bin/i3lock -i /home/jamie/.currbg

[Install]
# Ensure that this is called when we're trying to suspend the machine
WantedBy=suspend.target

Once I've told systemd to enable this service by default (by running sudo systemctl enable i3lock.service as a one-time thing), systemd then knows that it needs to run this command before the machine can go to sleep.

This makes things super easy, as now I can suspend by simply running systemctl suspend, or using a handy key combination to do the same, and it automagically locks my machine.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

Tags

This post was filed under articles.

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.