Create a systemd unit for i3lock on suspend

Posted on Tue 04 December 2018 in linux

See Creating a custom lockscreen with i3lock to setup the lockscreen script

Add a service file to run the script when the computer sleeps or suspends

Create a file /etc/systemd/system/screenlock@.service with the following contents. (Change the username to be your local user account name)

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

[Service]
User=username
Type=forking
Environment=DISPLAY=:0
ExecStartPre=
ExecStart=/usr/local/bin/lock_and_blur.sh

[Install]
WantedBy=suspend.target

You may want your computer to lock on sleep (for example if you are using hybrid-sleep ) In that case modify the above script to look like this:

[Unit]
Description=Starts i3lock on sleep
Before=sleep.target

[Service]
User=username
Type=forking
Environment=DISPLAY=:0
ExecStartPre=
ExecStart=/usr/local/bin/lock_and_blur.sh

[Install]
WantedBy=sleep.target

Enable the service by running:
sudo systemctl enable screenlock@username.service again replacing username with the linux account username