It took me a few tries before figuring this one out. But I eventually found the [the easy] solution in an old thread on the Arch Linux forums [Link].
I started out by creating the file
/etc/systemd/system/i3lock.service with the following content:
[Unit] Description=Lock screen before suspend Before=sleep.target [Service] User=johan Type=forking Environment=DISPLAY=:0 ExecStart=/home/johan/Scripts/i3lock.sh [Install] WantedBy=sleep.target
I then enabled the service with the command:
# systemctl enable i3lock.service
That’s it! And for those curious, this is what my
i3lock.sh looks like:
#!/bin/bash icon="$HOME/.config/i3/lock.png" img="$HOME/.cache/i3lock.png" # Take a screenshot for our background scrot $img # Pixelate the background convert $img -scale 10% -scale 1000% $img # Add the lock-icon convert $img $icon -gravity center -composite $img # Finally run i3lock itself i3lock -u -i $img
It will now lock my screen before it goes to sleep, so it’s locked when it wakes up again.