You are not logged in.

#1 2021-02-12 05:52:36

omernaveedxyz
Member
Registered: 2020-12-31
Posts: 34

[Solved] Starting Slock from suspend.service 'cannot open display'

I have Slock installed as my screen locker. Manually calling slock from my terminal using

slock

works fine, but when I tried to create a suspend service using systemd to automatically start slock when my laptop goes into suspend, I get the following issues.

● suspend@omer.service - User suspend actions
     Loaded: loaded (/etc/systemd/system/suspend@omer.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Thu 2021-02-11 23:45:25 CST; 4min 14s ago
    Process: 1353 ExecStart=slock (code=exited, status=1/FAILURE)

Feb 11 23:45:25 OMER-LAPTOP-LINUX systemd[1]: Starting User suspend actions...
Feb 11 23:45:25 OMER-LAPTOP-LINUX slock[1353]: slock: cannot open display
Feb 11 23:45:25 OMER-LAPTOP-LINUX systemd[1]: suspend@omer.service: Control process exited, code=exited, status=1/FAILURE
Feb 11 23:45:25 OMER-LAPTOP-LINUX systemd[1]: suspend@omer.service: Failed with result 'exit-code'.
Feb 11 23:45:25 OMER-LAPTOP-LINUX systemd[1]: Failed to start User suspend actions.

/etc/systemd/system/suspend@omer.service

[Unit]
Description=User suspend actions
Before=sleep.target

[Service]
User=%I
Type=forking
# Environment=DISPLAY=:0
ExecStart=/usr/bin/slock
ExecStartPost=/usr/bin/sleep 1

[Install]
WantedBy=sleep.target

Followed guide here: https://wiki.archlinux.org/index.php/Po … leep_hooks

Attempted solutions:
- Adding 'export DISPLAY=:0' no luck (running 'echo $DISPLAY' returns ':0' regardless
- Adding/Removing Environment=DISPLAY=:0 as shown above
- Adding 'ExecStartPre=/usr/bin/xset dpms force suspend' as shown here: https://wiki.archlinux.org/index.php/Slock results in 'xset: cannot open display'

Solution:
This ended up being partly my fault. I have my .Xauthority file move to XDG_RUNTIME_DIR/Xauthority through .zshenv so that it doesn't clutter my home. Slock was looking for this file so I just had to specify it as an environment variable in the service file.

Suspend@omer.service

[Unit]
Description=User suspend actions
Before=suspend.target

[Service]
User=omer
Type=forking
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/run/user/1000/Xauthority"
ExecStart=slock
ExecStartPost=/usr/bin/sleep 1

[Install]
WantedBy=suspend.target

Last edited by omernaveedxyz (2021-02-20 00:15:05)

Offline

Board footer

Powered by FluxBB