Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lock screen after suspend with i3lock and systemd
# file /etc/systemd/system/wakelock.service
[Unit]
Description=Lock the screen on resume from suspend
[Service]
User=victor
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/i3lock
[Install]
WantedBy=sleep.target
WantedBy=suspend.target
@jakdept

This comment has been minimized.

Copy link

@jakdept jakdept commented Aug 4, 2018

Maybe you are still working on this, maybe you are not. However, I got this working:

jack@jack-laptop:~|⇒  cat /etc/systemd/system/wakelock.service
[Unit]
Description=Lock the screen on resume from suspend

[Service]
User=jack
Type=forking
Environment=DISPLAY=:0
ExecStart=/home/jack/bin/lock

[Install]
WantedBy=sleep.target suspend.target
Before=sleep.traget suspend.target
jack@jack-laptop:~|⇒  cat ~/bin/lock 
#!/bin/bash
pgrep i3lock || i3lock -i ~/Pictures/lockscreen.png
jack@jack-laptop:~|⇒  cat ~/.i3/config 
# lock the current window
bindsym $mod+Escape exec ~/bin/lock
bindsym $mod+Shift+Escape exec ~/bin/lock && systemctl suspend

If you're running systemd, that should work no matter what, but systemctl suspend is probably a more portable choice - wherever systemd is.

@wolfv

This comment has been minimized.

Copy link

@wolfv wolfv commented Aug 11, 2018

any chance you know if one can only lock if suspended for longer than x minutes? that would be a killer power save mode, because actually waking up from suspend doesn't take that long on modern hardware.

@berkaycagir

This comment has been minimized.

Copy link

@berkaycagir berkaycagir commented Nov 15, 2018

Maybe you are still working on this, maybe you are not. However, I got this working:

jack@jack-laptop:~|⇒  cat /etc/systemd/system/wakelock.service
[Unit]
Description=Lock the screen on resume from suspend

[Service]
User=jack
Type=forking
Environment=DISPLAY=:0
ExecStart=/home/jack/bin/lock

[Install]
WantedBy=sleep.target suspend.target
Before=sleep.traget suspend.target
jack@jack-laptop:~|⇒  cat ~/bin/lock 
#!/bin/bash
pgrep i3lock || i3lock -i ~/Pictures/lockscreen.png
jack@jack-laptop:~|⇒  cat ~/.i3/config 
# lock the current window
bindsym $mod+Escape exec ~/bin/lock
bindsym $mod+Shift+Escape exec ~/bin/lock && systemctl suspend

If you're running systemd, that should work no matter what, but systemctl suspend is probably a more portable choice - wherever systemd is.

Before value should be in the [Unit] section, otherwise systemd gives out this error message:

Nov 15 12:43:14 berkay-ThinkPad-x230 systemd[1]: /etc/systemd/system/wakelock.service:12: Unknown lvalue 'Before' in section 'Install'

Fixed version is like this:

→ cat /etc/systemd/system/wakelock.service 
[Unit]
Description=Lock the screen on resume from suspend
Before=sleep.target suspend.target

[Service]
User=berkay
Type=forking
Environment=DISPLAY=:0
ExecStart=/home/berkay/bin/lock

[Install]
WantedBy=sleep.target suspend.target
@wis

This comment has been minimized.

Copy link

@wis wis commented Mar 30, 2019

why would you name the user a system config/etc file?
and use etckeeper to keep track of your /etc/ (system config files) in a repo
replace the equivalent lines with the following, and rename to /etc/systemd/system/wakelock@.service

User=%i
ExecStart=/usr/bin/i3lock
; or ExecStart=/home/%i/.scripts/your-lockscreen-script

and enable it with systemctl enable suspend@<YOUR-USER-NAME>.service

@hollingsworthd

This comment has been minimized.

Copy link

@hollingsworthd hollingsworthd commented Sep 12, 2020

There's a typo above on "traget".

Anyhow here's an alternate version that works:

[Unit]
Description=Lock the screen
Before=suspend.target

[Service]
User=%i
Type=forking
Environment=DISPLAY=":0"
ExecStart=i3lock

[Install]
WantedBy=suspend.target

As mentioned above, place this in a new file at /etc/systemd/system/wakelock@.service and run: systemctl enable wakelock@<YOUR USERNAME>.service && systemctl daemon-reload


And to keep the lock screen from flashing the desktop:

#!/usr/bin/env bash

if [ "$1" == "pre" ]; then
  sleep 2
fi

This can go in a new file at /lib/systemd/system-sleep/blank and then run: sudo chmod +x /lib/systemd/system-sleep/blank


Also heads up, compositors might not play nicely with lock screens. For instance if there's an option to "display fullscreen overlay windows directly" try disabling it if you can't see the lock screen.

@simonfontana

This comment has been minimized.

Copy link

@simonfontana simonfontana commented Oct 26, 2020

Thanks everyone. Just a couple of notes. I had to use the absolute path for i3lock and my DISPLAY was set to ":1" (I'm using Ubuntu 18.04). So I simply changed two lines from @hollingsworthd's comment:

Environment=DISPLAY=":1"
ExecStart=/usr/bin/i3lock --color='#111111'
@jwhendy

This comment has been minimized.

Copy link

@jwhendy jwhendy commented Mar 2, 2021

I've seen a bunch of variations on this concept, and wonder if anyone here could comment on:

  • Type=simple vs. Type=forking
  • why some use suspend.target and/or sleep.target in Before and/or WantedBy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment