Skip to content

Instantly share code, notes, and snippets.

@Raymo111
Last active April 22, 2024 02:18
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Raymo111/91ffd256b7aca6a85e8a99d6331d3b7b to your computer and use it in GitHub Desktop.
Save Raymo111/91ffd256b7aca6a85e8a99d6331d3b7b to your computer and use it in GitHub Desktop.
How to use i3lock to wake from suspend state

How to use i3lock to wake from suspend state

By Raymond Li (Raymo111), September 3rd, 2019

Last updated 28 August 2021

Table of Contents

Step 0: Preparation
Step 1: Create the lock script
Step 2: Create the systemd module
Step 3: Enable the systemd module

Step 0: Preparation

Disable any preexisting screen locker. I'm using KDE on Arch Linux, so my options for kscreenlocker look like this: The options lock screen automatically after and lock screen on resume are unchecked

If you don't have it already, make sure you install i3lock. I highly recommend installing i3lock-color.

Step 1: Create the lock script

Create a file at ~/scripts/i3lock.sh, with the following content:

#!/bin/bash
i3lock # Whatever i3lock options you want to use. Use i3lock -h for a list of options

Step 2: Create the systemd module

Create a file using sudo at /etc/systemd/system/wakelock@.service, with the following content:

[Unit]
Description=Lock the screen on resume from suspend
Before=sleep.target suspend.target

[Service]
User=%i
Type=forking
Environment=DISPLAY=:0
ExecStart=/home/%i/scripts/i3lock.sh

[Install]
WantedBy=sleep.target suspend.target

Step 3: Enable the systemd module

Enable the systemd module you just created with:

sudo systemctl enable wakelock@<YOUR-USER-NAME> --now

i3lock will now run once as a demo, then when you wake from suspend, i3lock will automatically start.

That's it! If you make any changes and want them reflected immediately without restart, simply do:

sudo systemctl restart wakelock@<YOUR-USER-NAME>

If my tutorial helped you, or if you encountered any errors or have any suggestions for me, leave a comment below to let me know!

@Pest0
Copy link

Pest0 commented Nov 14, 2021

Great guide! 👍

@Raymo111
Copy link
Author

Great guide! 👍

Thanks! Means a lot to hear that this was useful for others. 🙂

@SafwanLjd
Copy link

SafwanLjd commented Dec 18, 2021

For some reason this works with programs like slock, but not with i3lock, even when I set it to /usr/bin/i3lock directly without any options

@Raymo111
Copy link
Author

@SafwanLjd do you have vanilla i3lock or i3lock-color installed? I use this every day with i3lock-color, so it definitely works for me?

@SafwanLjd
Copy link

SafwanLjd commented Dec 18, 2021

@Raymo111 I'm aware that it should work, but I don't know why it doesn't, and yes I do have i3lock-color installed and I do have my own script with a bunch of options (https://github.com/SafwanLjd/BlurLocker) and neither my script nor running /bin/i3lock work through the method in this gist; However, as mentioned above, when trying the same method with slock it also works.

For now I just use xss-lock to sort of achieve the same behavior and it works almost flawlessly (and yes, I did kill xss-lock when trying this method, it still doesn't work), but I would love to be using this method, I just don't know why it won't work...

@Raymo111
Copy link
Author

Interesting. Since I maintain i3lock-color I think I should probably figure out why slock works but i3lock-color doesn't. Can you try with vanilla i3lock?

@SafwanLjd
Copy link

SafwanLjd commented Dec 18, 2021

@Raymo111 I just noticed that I get this error when I try using i3lock-color (and i3lock, I've just tested it), but not when using slock
the error

journalctl output:
journalctl output

I'm no SystemD expert, so I'm not sure what the problem is

@Raymo111
Copy link
Author

The errors are indeed from i3lock-color:

Authorization required, but no authorization protocol specified
i3lock: Could not connect to X11, maybe you need to set DISPLAY?

Are you missing this line in the config?:

Environment=DISPLAY=:0

@SafwanLjd
Copy link

@Raymo111 Nope, it is definitely there, I copied the whole thing from the gist

@gianmarcogg03
Copy link

I'm using betterlockscreen and it shows the lockscreen before going to standby, and when I enter the password that's when the computer goes to standby. When I wake it up it doesn't show the lockscreen.

@omargtdev
Copy link

@gianmarcogg03 Yes, I have the same problem.

@benlarkins
Copy link

@gianmarcogg03 @omargtdev with betterlockscreen setting Type=forking causes that issue. You should set Type=simple instead. See this issue:
betterlockscreen/betterlockscreen#277

@beyond9thousand
Copy link

got this to work but my desktop is visible for a few seconds before i3lock takes over

also i had to use 'After' instead of 'Before'

@rkvirani
Copy link

rkvirani commented Mar 4, 2023

Appreciate you spending the time to document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment