systemd to lock screen on suspend as X session owner?

2

I have recently moved to using systemd to manage power control.

In order to get my screen to be locked when returning from suspend, I created /etc/systemd/system/i3lock.service with these contents

[Unit]
Description=i3lock
Before=sleep.target

[Service]
User=fommil
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/i3lock -c 000000

[Install]
WantedBy=sleep.target

and then ran systemctl enable i3lock.service.

But this means the lock is always for the fommil user.

How does one set up a service that will run the screen locker as the user who currently owns the X session?

fommil

Posted 2014-06-29T15:40:36.047

Reputation: 337

I don't think that's a job for the system systemd. And the user is not the only problem, the DISPLAY environment variable is another one. – Cristian Ciupitu – 2014-06-29T21:07:44.010

2

You might want to try i3lock-wait-for-suspend by olvaffe or i3lock-wait-for-suspend by kanru.

– Cristian Ciupitu – 2014-06-29T21:25:57.770

Answers

1

You should use a user unit for that instead of a system unit. Put it in /etc/systemd/user/, if you want it to be available (not enabled) for all users, or $HOME/.config/systemd/user/, if you want it to be available for that user only.

Then you can remove the User= option (and probably the Environment= option as well, you can run systemctl show-environment as the current user to see if the required env vars have already been imported), and then enable the service with systemd --user enable i3clock. (Or sudo systemd --global enable i3lock if it's available for all users and you want it to be enabled for all users.)

Tom Yan

Posted 2014-06-29T15:40:36.047

Reputation: 4 744