Systemd xlock and suspend

Hi all,

I want to use systemd to lock my screen after I suspend my system. I’m not using any common desktop environment but i3 as a lightweight window manager. It has now power management so my system doesn’t do that automatically. I wanted to write a systemd service which handles this problem.

I placed the service under /etc/systemd/system/xlock.service:


[Unit]
Description=Lock X session using xlock

[Service]
User=philipp
Environment=DISPLAY=:0
ExecStart=/usr/bin/xlock -mode strange -bg black -fg white

[Install]
WantedBy=sleep.target

But this doesn’t work.
The error messages i get:


Aug 01 16:18:43 tesla.site xlock[19261]: No protocol specified
Aug 01 16:18:43 tesla.site xlock[19261]: xlock: unable to open display .

I don’t know how to get this work. I set the display variable and everything else looks good. I think openSUSE implements suspend a bit different than archlinux does, because on archlinux it seems to work:
https://bbs.archlinux.org/viewtopic.php?id=148829

Does anybody has an idea?

System: openSUSE 12.3 x86_64
WM: i3

On Thu 01 Aug 2013 02:36:05 PM CDT, seilerphilipp wrote:

Hi all,

I want to use systemd to lock my screen after I suspend my system. I’m
not using any common desktop environment but i3 as a lightweight window
manager. It has now power management so my system doesn’t do that
automatically. I wanted to write a systemd service which handles this
problem.

I placed the service under /etc/systemd/system/xlock.service:

Code:

[Unit]
Description=Lock X session using xlock

[Service]
User=philipp
Environment=DISPLAY=:0
ExecStart=/usr/bin/xlock -mode strange -bg black -fg white

[Install]
WantedBy=sleep.target


But this doesn’t work.
The error messages i get:

Code:

Aug 01 16:18:43 tesla.site xlock[19261]: No protocol specified
Aug 01 16:18:43 tesla.site xlock[19261]: xlock: unable to open
display .

I don’t know how to get this work. I set the display variable and
everything else looks good. I think openSUSE implements suspend a bit
different than archlinux does, because on archlinux it seems to work:
[SOLVED] xlock+suspend with systemd / Applications & Desktop Environments / Arch Linux Forums

Does anybody has an idea?

System: openSUSE 12.3 x86_64
WM: i3

Hi
What about using i3lock?
http://software.opensuse.org/package/i3lock

systemd service


Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.3 (x86_64) Kernel 3.7.10-1.16-desktop
up 14:49, 3 users, load average: 0.59, 0.35, 0.27
CPU AMD E2-1800@1.70GHz | GPU Radeon HD 7340

It’s always the same. I can fix my problems by myself, after I start a forum thread. Even if i try to fix this problem for a few days.
The problem was, that not the DISPLAY variable isn’t set, but the XAUTHORITY variable. But to get safe i added the parameter “-display :0” to the xlock command line.

My systemd service now looks like this:


[Unit]
Description=Lock X session using xlock

[Service]
User=philipp
Environment=XAUTHORITY=/run/lightdm/philipp/xauthority
ExecStart=/usr/bin/xlock -mode strange -bg black -fg white -display :0

[Install]
WantedBy=sleep.target

/e: @malcomlewis:

I think this would be the same result as xlock was. I tried xtrlock slock and xlock.

I think the point of the matter is the XAUTHORITY variable.

But nevertheless thank your for the quick answer :wink:
](https://forums.opensuse.org/members/malcolmlewis.html)

I can try to explain the second message.

As you run this somewhere from the background, the program xlock has no idea what to do when it comes to interaction with an X-server. Which one? Linux is a multi-user system and there can be many X sessions running at the same time. As a general convention the environment variable DISPLAY has the value of the display (host identification and display number) that is to be used by any X program. For most simple cases like only one user being loged in localy on the first display of a system (remember that X on openSUSE allows more logical displays on the physical screen you have) is

henk@boven:~> echo $DISPLAY
:0
henk@boven:~>

Thus first thing to try is to changte your command to

DISPLAY=:0 /usr/bin/xlock -mode strange -bg black -fg white

Allthough that is not 100% tight as doing this you in fact deny that there can be multiple users using X (local and remote) and multiple screens on the sytem. It even does not cope for no user loged in using display :0.

A second problem could then be that the program is not allowed to do something with diplay :0. After all, most people want to be sure that no outsiders can attach there display, open windows at will and possibly even reading all their keyboard typing. Thus there is security implemented there. A user can allow/deny usage of his display from local/remote on a per host and per user base with the tool

xhost

Check what your current situation is with

henk@boven:~> xhost
access control disabled, clients can connect from any host
henk@boven:~>

and read

man xhost

Hm, it seems I took to long to type the above and in the meantime you solved it all. Very good.

That’s the wrong tool to do it. systemd manages system services, it does not manage user sessions (yet). Actually, all desktop environments can do it, and those I worked with do it by default; why do you need to reinvent the wheel?

replace xlock with systemd in the sentence you have cited.
But in fact systemd is the right place to configure actions/commands/whatelse which depends on a suspend event/target

And for working with systemd, including creating service files, have a look at my bash script you can find here: SysdCmd - systemd Command Help/Config Editor - Blogs - openSUSE Forums

Thank You,