Aaron Lauterer

Start i3lock on Sleep with SystemD

Ever wondered how to automatically lock your screen when you put your laptop to sleep? Like me you most likely found a SystemD unit file looking a lot like the one below. What annoyed me though was that the lock command would be executed after the system reached the sleep target, ergo when waking up. This resulted in a brief flicker where my screen was visible before i3lock kicked in.

Adding the third line, Before=sleep.target helped to solve that problem.

[Unit]
Description=Starts i3lock at suspend time
Before=sleep.target

[Service]
User=%I
Type=forking
Environment=DISPLAY=:0
ExecStart=<path to your lock script>

[Install]
WantedBy=suspend.target

Enable it with:

$ systemctl enable screenlock@<username>
Got any hints or questions? blog@aaronlauterer.com