My naive approach was the following:
[Unit] Description=sleep.target of a systemd user session Before=sleep.target StopWhenUnneeded=yes [Service] Type=oneshot User=%I RemainAfterExit=yes ExecStart=/usr/bin/systemctl --user start sleep.target ExecStop=/usr/bin/systemctl --user stop sleep.target [Install] WantedBy=sleep.target
[Unit] Description=Sleep Timer Target StopWhenUnneeded=yes
Unfortunately I got the following error:
$ systemctl status user-sleep@joerg firstname.lastname@example.org - sleep.target of a systemd user session Loaded: loaded (/etc/systemd/system/user-sleep@.service; enabled) Active: failed (Result: exit-code) since Mo 2013-06-10 12:30:41 CEST; 43min ago Process: 10503 ExecStart=/usr/bin/systemctl --user start sleep.target (code=exited, status=1/FAILURE) Jun 10 12:30:41 turing-machine systemd: Service email@example.com is not needed anymore. Stopping. Jun 10 12:30:41 turing-machine systemd: firstname.lastname@example.org: main process exited, code=exited, status=1/FAILURE Jun 10 12:30:41 turing-machine systemd: Failed to start sleep.target of a systemd user session. Jun 10 12:30:41 turing-machine systemd: Unit email@example.com entered failed state.
Can I use "systemctl --user" only at a login shell and not in a systemd unit? Because using "systemd --user" from tty works great.
My use case is to logout gajim before suspend. This cannot be done in a normal .service file, because it does require a connection to the current dbus session.
Thank you for your idea! I''ve successfully configured this.
systemctl communicates with systemd via D-Bus. For --user, it's the session D-Bus, so you need to set that right. Try to add this to your user-sleep@.service and see if it works for you. Also you'll need to use user ID instead of user name.