Difference between revisions of "Systemd/User"

From ArchWiki
Jump to: navigation, search
(Using systemd --user To Manage Your Session)
m (Note about X applications)
(29 intermediate revisions by 11 users not shown)
Line 3: Line 3:
 
[[Category:Boot process]]
 
[[Category:Boot process]]
 
[[es:Systemd/User]]
 
[[es:Systemd/User]]
 +
[[it:Systemd/User]]
 
{{Article summary start}}
 
{{Article summary start}}
 
{{Article summary text|Covers how to set up [[systemd]] user sessions.}}
 
{{Article summary text|Covers how to set up [[systemd]] user sessions.}}
Line 10: Line 11:
  
 
[[systemd]] offers users the ability to run an instance of [[systemd]] to manage their session and services. This allows users to start, stop, enable, and disable units found within certain directories when systemd is run by the user. This is convenient for daemons and other services that are commonly run as a user other than root or a special user, such as [[mpd]].
 
[[systemd]] offers users the ability to run an instance of [[systemd]] to manage their session and services. This allows users to start, stop, enable, and disable units found within certain directories when systemd is run by the user. This is convenient for daemons and other services that are commonly run as a user other than root or a special user, such as [[mpd]].
 +
 +
{{Note|systemd --user sessions are not compatible with -ck patchset}}
  
 
== Setup ==
 
== Setup ==
  
 
=== startx ===
 
=== startx ===
 +
 +
{{Note|This step is unnecessary if you plan to use autologin.}}
  
 
Users should first set up systemd-logind to manage their session. If [[systemd]] is running as the system init daemon, then this is already happening.
 
Users should first set up systemd-logind to manage their session. If [[systemd]] is running as the system init daemon, then this is already happening.
  
Next, the user must launch systemd by putting the following in their {{ic|~/.xinitrc}} '''before the exec line''':
+
Next, the user must launch systemd by putting the following in their {{ic|~/.xinitrc}}.
systemd --user &
+
{{bc|systemd --user}}
 +
If the user is not launching the window manager through systemd --user, then {{bc|systemd --user &}} should be used and launched like anything else in {{ic|~/.xinitrc}}, before execing the window manager.
  
 
After starting X, the user can check whether their session is now being managed by systemd-logind with the following command:
 
After starting X, the user can check whether their session is now being managed by systemd-logind with the following command:
$ loginctl --no-pager show-session $XDG_SESSION_ID | grep Active
+
 
If this command prints {{ic|<nowiki>Active=yes</nowiki>}}, then the user is now using systemd-logind to manage their session. The user should remove any instances of '''ck-launch-session''' or '''dbus-launch''' from their {{ic|~/.xinitrc}}, as those commands are unneeded.
+
{{bc|<nowiki>
 +
$ loginctl --no-pager show-session $XDG_SESSION_ID | grep Active
 +
</nowiki>}}
 +
 
 +
If this command prints {{ic|1=Active=yes}}, then the user is now using systemd-logind to manage their session. The user should remove any instances of '''ck-launch-session''' or '''dbus-launch''' from their {{ic|~/.xinitrc}}, as those commands are unneeded.
  
 
=== Display Managers ===
 
=== Display Managers ===
 
All of the major display managers are now using systemd-logind by default, so the {{ic|loginctl}} command from the previous section should work as stated. A user simply has to add {{ic|systemd --user}} as a program to be started by their desktop environment.
 
All of the major display managers are now using systemd-logind by default, so the {{ic|loginctl}} command from the previous section should work as stated. A user simply has to add {{ic|systemd --user}} as a program to be started by their desktop environment.
  
=== Using systemd --user To Manage Your Session ===
+
==== GNOME 3 (using GDM) ====
 +
For users who wish to have GDM/GNOME 3 auto-start their {{ic|systemd --user}} session upon login, they just need to add a special log in session for this:
 +
{{hc|/usr/share/xsessions/gnome-systemd.desktop|<nowiki>
 +
[Desktop Entry]
 +
Type=Application
 +
Name=systemd
 +
Comment=Runs 'systemd' as a user instance.
 +
Exec=/usr/lib/systemd/systemd --user
 +
</nowiki>}}
  
Systemd has many amazing features, one of which is the ability to track programs using cgroups (by running {{ic|systemctl status}}). While awesome for a pid 1 process to do, it is also extremely useful for users, and having it set up and initialize user programs, all the while tracking what is in each cgroup is even more amazing.
+
Make sure to choose the {{ic|systemd}} session option at the GDM login screen.
 +
{{Note|This has only been tested with a pure GDM and GNOME 3 setup. For other set ups, YYMV. This method does not need the systemd user-session scripts installed.}}
  
See [http://blog.gtmanfred.com/?p=26 gtmanfred's guide] for the original information. For a more updated version of this see [https://bitbucket.org/KaiSforza/systemd-user-units/wiki/Home KaiSforza's bitbucket wiki].
+
=== Using systemd --user To Manage Your Session ===
  
All of your systemd user units will go to $HOME/.config/systemd/user. These units take precedence over units in other systemd unit directories.
+
Systemd has many amazing features, one of which is the ability to track programs using cgroups (by running {{ic|systemctl status}}). While awesome for a '''pid 1''' process to do, it is also extremely useful for users, and having it set up and initialize user programs, all the while tracking what is in each cgroup is even more amazing.
  
There are two packages you need to get this working, both currently available from the AUR: {{AUR|xorg-launch-helper}} and optionally, {{AUR|user-session-units}} if you want to have autologin working.  
+
All of your systemd user units will go to {{ic|$HOME/.config/systemd/user}}. These units take precedence over units in other systemd unit directories.
 +
 
 +
There are two packages you need to get this working, both currently available from the [[AUR]]: {{AUR|xorg-launch-helper}} and optionally, {{AUR|user-session-units}} if you want to have autologin working.  
  
 
Next is setting up your targets. You should set up two, one for window manager and another as a default target. The window manager target should be populated like so:
 
Next is setting up your targets. You should set up two, one for window manager and another as a default target. The window manager target should be populated like so:
  
[Unit]
+
{{hc|$HOME/.config/systemd/user/wm.target|<nowiki>
Description=Window manager target
+
[Unit]
Wants=xorg.target
+
Description=Window manager target
Wants=myStuff.target
+
Wants=xorg.target
Requires=dbus.socket
+
Wants=mystuff.target
AllowIsolate=true
+
Requires=dbus.socket
+
AllowIsolate=true
[Install]
+
Alias=default.target
+
  
 +
[Install]
 +
Alias=default.target
 +
</nowiki>}}
 
This will be the target for your graphical interface.
 
This will be the target for your graphical interface.
  
I put together a second target called mystuff.target. This will be 'WantedBy' all services but your window manager:
+
Put together a second target called {{ic|mystuff.target}}. All services but your window manager should contain a {{ic|WantedBy}} line, under {{ic|[Install]}}, pointing at this unit.
  
[Unit]
+
{{hc|$HOME/.config/systemd/user/mystuff.target|<nowiki>
Description=Xinitrc Stuff
+
[Unit]
Wants=i3wm.target
+
Description=Xinitrc Stuff
+
Wants=wm.target
[Install]
+
Alias=default.target
+
  
Link this unit to default.target. When you start {{ic|systemd --user}}, it will start this target.  
+
[Install]
 +
Alias=default.target
 +
</nowiki>}}
 +
 
 +
Link this unit to {{ic|default.target}}. When you start {{ic|systemd --user}}, it will start this target.  
  
 
Next you need to begin writing services. First you should throw together a service for your window manager:
 
Next you need to begin writing services. First you should throw together a service for your window manager:
  
[Unit]
+
{{hc|$HOME/.config/systemd/user/YOUR_WM.service|<nowiki>
Description=your window manager service
+
[Unit]
Before=mystuff.target
+
Description=your window manager service
After=xorg.target
+
Before=mystuff.target
+
After=xorg.target
[Service]
+
Requires=xorg.target
Requires=xorg.target
+
 
#Environment=PATH=uncomment:to:override:your:PATH
+
[Service]
Environment=DISPLAY=:0
+
#Environment=PATH=uncomment:to:override:your:PATH
ExecStart=/full/path/to/wm/executable
+
ExecStart=/full/path/to/wm/executable
Restart=always
+
Restart=always
RestartSec=10
+
RestartSec=10
 
   
 
   
[Install]
+
[Install]
WantedBy=wm.target
+
WantedBy=wm.target
 +
</nowiki>}}
  
Note the [Install] section includes a 'WantedBy' part. When using {{ic|systemctl --user enable}} it will link this as $HOME/.config/systemd/user/i3wm.target.wants/i3.service, allowing it to be started at login. I would recommend enabling this service, not linking it manually.
+
{{Note|The {{ic|[Install]}} section includes a 'WantedBy' part. When using {{ic|systemctl --user enable}} it will link this as {{ic|$HOME/.config/systemd/user/wm.target.wants/i3.service}}, allowing it to be started at login. Is recommended enabling this service, not linking it manually.}}
  
You can fill your user unit directory with a plethora of services, including ones for mpd, gpg-agent, offlineimap, parcellite, pulse, tmux, urxvtd, xbindkeys and xmodmap to name a few.
+
You can fill your user unit directory with a plethora of services, including ones for '''mpd''', '''gpg-agent''', '''offlineimap''', '''parcellite''', '''pulse''', '''tmux''', '''urxvtd''', '''xbindkeys''' and '''xmodmap''' to name a few.
 +
 
 +
===Auto-login===
  
 
If you want to have systemd automatically log you in on boot, then you can use the unit in user-session-units to do so. Enabling a screen locker for will stop someone from booting your computer into a nice, logged in session.  
 
If you want to have systemd automatically log you in on boot, then you can use the unit in user-session-units to do so. Enabling a screen locker for will stop someone from booting your computer into a nice, logged in session.  
  
If you installed user-session-units as listed above, then you must copy user-session@.service to {{ic|/etc/systemd/system}} directory and edit the user-session@.service (not the user-session@yourloginname.service) and edit this line:  
+
If you installed user-session-units as listed above, then you must copy {{ic|/usr/lib/systemd/system/user-session@.service}} to {{ic|/etc/systemd/system/user-session@yourloginname.service}}) and edit these lines:  
  
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%I/dbus/user_bus_socket
+
{{bc|1=Environment=XDG_RUNTIME_DIR=/run/user/%I
 +
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%I/dbus/user_bus_socket}}
  
to this: (Note the subtle change where the {{ic|%I}} become {{ic|%U}})
+
to this:  
  
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%U/dbus/user_bus_socket
+
{{bc|1=Environment=XDG_RUNTIME_DIR=/run/user/%U
 +
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%U/dbus/user_bus_socket}}
  
 +
{{Note|Notice the subtle change where {{ic|%I}} is replaced by {{ic|%U}}}}
 
As well as an install section:
 
As well as an install section:
  
[Install]
+
{{bc|1=
WantedBy=graphical.target
+
[Install]
 +
WantedBy=graphical.target
 +
}}
  
You will need to patch systemd to do this, so either using systemd-git from after [http://cgit.freedesktop.org/systemd/systemd/commit/?id=067d851d30386c553e3a84f59d81d003ff638b91 commit 067d851d] or patch it into systemd with the [[ABS]]. After 197, it should be in the mainline systemd.
+
or if you have no login manager:
  
Add this line to /etc/pam.d/login and /etc/pam.d/system-auth:
+
{{bc|1=
 +
[Install]
 +
WantedBy=getty.target
 +
}}
  
session    required    pam_systemd.so
+
Add this line to {{ic|/etc/pam.d/login}} and {{ic|/etc/pam.d/system-auth}}:
  
Because user-session@.service starts on tty1, you will need to add Conflicts={{ic|getty@tty1.service}} to the service file.
+
{{bc|session    required    pam_systemd.so}}
  
One of the most important things you can add to the service files you will be writing is the use of Before= and After= in the [Unit] section. These two parts will determine the order things are started. Say you have a graphical application you want to start on boot, you would put `After=xorg.target' into your unit. Say you start ncmpcpp, which requires mpd to start, you can put `After=mpd.service' into your ncmpcpp unit. You will eventually figure out exactly how this needs to go either from experience or from reading the systemd manual pages. Starting with [http://www.freedesktop.org/software/systemd/man/systemd.unit.html systemd.unit(5)] is a good idea.
+
Because {{ic|user-session@.service}} starts on tty1, you will need to add {{ic|1=Conflicts=getty@tty1.service}} to the service file, if it doesn't exist already.
 +
 
 +
Once this is done, {{ic|systemctl --user enable}} {{ic|YOUR_WM.service}}
 +
 
 +
One of the most important things you can add to the service files you will be writing is the use of {{ic|1=Before=}} and {{ic|1=After=}} in the {{ic|[Unit]}} section. These two parts will determine the order things are started. Say you have a graphical application you want to start on boot, you would put {{ic|1=After=xorg.target}} into your unit. Say you start '''ncmpcpp''', which requires '''mpd''' to start, you can put {{ic|1=After=mpd.service}} into your ncmpcpp unit. You will eventually figure out exactly how this needs to go either from experience or from reading the systemd manual pages. Starting with [http://www.freedesktop.org/software/systemd/man/systemd.unit.html systemd.unit(5)] is a good idea.
 +
 
 +
=== Other use cases ===
 +
 
 +
==== Persistent terminal multiplexer ====
 +
 
 +
You may wish your user session to default to running a terminal multiplexer, such as [[GNU Screen]] or [[Tmux]], in the background rather than logging you in to a window manager session. Separating login from X login is most likely only useful for those who boot to a TTY instead of to a display manager (in which case you can simply bundle everything you start in with myStuff.target).
 +
 
 +
To create this type of user session, procede as above, but instead of creating wm.target, create multiplexer.target:
 +
 
 +
{{bc|1=
 +
[Unit]
 +
Description=Terminal multiplexer
 +
Documentation=info:screen man:screen(1) man:tmux(1)
 +
After=cruft.target
 +
Wants=cruft.target
 +
 
 +
[Install]
 +
Alias=default.target
 +
}}
 +
 
 +
{{ic|cruft.target}}, like {{ic|mystuff.target}} above, should start anything you think should run before tmux or screen starts (or which you want started at boot regardless of timing), such as a GnuPG daemon session.
 +
 
 +
You then need to create a service for your multiplexer session. Here's a sample service, using tmux as an example and sourcing a gpg-agent session which wrote its information to /tmp/gpg-agent-info. This sample session, when you start X, will also be able to run X programs, since DISPLAY is set.
 +
 
 +
{{bc|1=
 +
[Unit]
 +
Description=tmux: A terminal multiplixer Documentation=man:tmux(1)
 +
After=gpg-agent.service
 +
Wants=gpg-agent.service
 +
 
 +
[Service]
 +
Type=forking
 +
ExecStart=/usr/bin/tmux start
 +
ExecStop=/usr/bin/tmux kill-server
 +
Environment=DISPLAY=:0
 +
EnvironmentFile=/tmp/gpg-agent-info
 +
 
 +
[Install]
 +
WantedBy=multiplexer.target
 +
}}
 +
 
 +
Once this is done, {{ic|systemctl --user enable}} {{ic|tmux.service}}, {{ic|multiplexer.target}} and any services you created to be run by {{ic|cruft.target}} and you should be set to go! Activated {{ic|user-session@.service}} as described above, but be sure to remove the {{ic|1=Conflicts=getty@tty1.service}} from {{ic|user-session@.service}}, since your user session will not be taking over a TTY. Congratulations! You have a running terminal multiplexer and some other useful programs ready to start at boot!
 +
 
 +
===== Starting X =====
 +
 
 +
You've probably noticed that, since the terminal multiplexer is now {{ic|default.target}}, X will not start automatically at boot. To start X, procede as above, but do not activate or manually link to {{ic|default.target}} {{ic|wm.target}}. Instead, assuming you're booting to a terminal, we'll simply be using a hackish workaround and masking {{ic|/usr/bin/startx}} with a shell alias:
 +
 
 +
{{bc|1=alias startx='systemctl --user start wm.target'}}
  
 
== User Services ==
 
== User Services ==
Line 118: Line 205:
 
A unit installed by a package that is meant to be run by a [[systemd]] user instance should install the unit to {{ic|/usr/lib/systemd/user/}}. The system adminstration can then modify the unit by copying it to {{ic|/etc/systemd/user/}}. A user can then modify the unit by copying it to {{ic|~/.config/systemd/user/}}.
 
A unit installed by a package that is meant to be run by a [[systemd]] user instance should install the unit to {{ic|/usr/lib/systemd/user/}}. The system adminstration can then modify the unit by copying it to {{ic|/etc/systemd/user/}}. A user can then modify the unit by copying it to {{ic|~/.config/systemd/user/}}.
 
=== Example ===
 
=== Example ===
The following is an example of a user version of {{ic|mpd.service}}:
+
The following is an example of a user version of the mpd service.
[Unit]
+
{{hc|mpd.service|<nowiki>
Description=Music Player Daemon
+
[Unit]
+
Description=Music Player Daemon
[Service]
+
ExecStart=/usr/bin/mpd --no-daemon
+
+
[Install]
+
WantedBy=default.target
+
  
 +
[Service]
 +
ExecStart=/usr/bin/mpd --no-daemon
 +
 +
[Install]
 +
WantedBy=default.target
 +
</nowiki>}}
 
=== Example with variables ===
 
=== Example with variables ===
 
The following is an example of a user version of {{ic|sickbeard.service}}, which takes into account variable home directories where SickBeard can find certain files:
 
The following is an example of a user version of {{ic|sickbeard.service}}, which takes into account variable home directories where SickBeard can find certain files:
[Unit]
+
{{hc|sickbeard.service|<nowiki>
Description=SickBeard Daemon
+
[Unit]
+
Description=SickBeard Daemon
[Service]
+
 
ExecStart=/usr/bin/env python2 /opt/sickbeard/SickBeard.py --config %h/.sickbeard/config.ini --datadir %h/.sickbeard
+
[Service]
+
ExecStart=/usr/bin/env python2 /opt/sickbeard/SickBeard.py --config %h/.sickbeard/config.ini --datadir %h/.sickbeard
[Install]
+
 
WantedBy=default.target
+
[Install]
 +
WantedBy=default.target
 +
</nowiki>}}
  
 
As detailed in {{ic|man systemd.unit}}, the {{ic|%h}} variable is replaced by the home directory of the user running the service. There are other variables that can be taken into account in the [[systemd]] manpages.
 
As detailed in {{ic|man systemd.unit}}, the {{ic|%h}} variable is replaced by the home directory of the user running the service. There are other variables that can be taken into account in the [[systemd]] manpages.
  
== External links ==
+
=== Note about X applications ===
 +
 
 +
Most X apps, need a {{ic|DISPLAY}} variable to run (so it's likely the first reason why your service files aren't starting), so you have to make sure to include it:
 +
 
 +
{{hc|$HOME/.config/systemd/user/parcellite.service|<nowiki>
 +
[Unit]
 +
Description=Parcellite clipboard manager
 +
 
 +
[Service]
 +
ExecStart=/usr/bin/parcellite
 +
Environment=DISPLAY=:0 # <= !
 +
 
 +
[Install]
 +
WantedBy=mystuff.target
 +
</nowiki>}}
 +
 
 +
A cleaner way though, it's '''not''' hard code the DISPLAY environment variable (specially if you run more than on display):
 +
 
 +
{{hc|$HOME/.config/systemd/user/x-app-template@.service|<nowiki>
 +
[Unit]
 +
Description=Your amazing and original description
 +
 
 +
[Service]
 +
ExecStart=/full/path/to/the/app
 +
Environment=DISPLAY=%i # <= !
 +
 
 +
[Install]
 +
WantedBy=mystuff.target
 +
</nowiki>}}
 +
 
 +
Then you can run it with:
 +
 
 +
systemctl --user {start|enable} x-app@your-desired-display.service # <= :0 in most cases
 +
 
 +
== See also ==
 +
* [http://blog.gtmanfred.com/?p=26 gtmanfred's guide - the original guide]
 +
* [https://bitbucket.org/KaiSforza/systemd-user-units/wiki/Home KaiSforza's bitbucket wiki]
 
* [https://github.com/grawity/systemd-user-units Collection of useful systemd user units]
 
* [https://github.com/grawity/systemd-user-units Collection of useful systemd user units]
 
* [https://bitbucket.org/KaiSforza/systemd-user-units More systemd user units]
 
* [https://bitbucket.org/KaiSforza/systemd-user-units More systemd user units]

Revision as of 16:24, 7 April 2013

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary end

systemd offers users the ability to run an instance of systemd to manage their session and services. This allows users to start, stop, enable, and disable units found within certain directories when systemd is run by the user. This is convenient for daemons and other services that are commonly run as a user other than root or a special user, such as mpd.

Note: systemd --user sessions are not compatible with -ck patchset

Setup

startx

Note: This step is unnecessary if you plan to use autologin.

Users should first set up systemd-logind to manage their session. If systemd is running as the system init daemon, then this is already happening.

Next, the user must launch systemd by putting the following in their ~/.xinitrc.

systemd --user
If the user is not launching the window manager through systemd --user, then
systemd --user &
should be used and launched like anything else in ~/.xinitrc, before execing the window manager.

After starting X, the user can check whether their session is now being managed by systemd-logind with the following command:

$ loginctl --no-pager show-session $XDG_SESSION_ID | grep Active

If this command prints Active=yes, then the user is now using systemd-logind to manage their session. The user should remove any instances of ck-launch-session or dbus-launch from their ~/.xinitrc, as those commands are unneeded.

Display Managers

All of the major display managers are now using systemd-logind by default, so the loginctl command from the previous section should work as stated. A user simply has to add systemd --user as a program to be started by their desktop environment.

GNOME 3 (using GDM)

For users who wish to have GDM/GNOME 3 auto-start their systemd --user session upon login, they just need to add a special log in session for this:

/usr/share/xsessions/gnome-systemd.desktop
[Desktop Entry]
Type=Application
Name=systemd
Comment=Runs 'systemd' as a user instance.
Exec=/usr/lib/systemd/systemd --user

Make sure to choose the systemd session option at the GDM login screen.

Note: This has only been tested with a pure GDM and GNOME 3 setup. For other set ups, YYMV. This method does not need the systemd user-session scripts installed.

Using systemd --user To Manage Your Session

Systemd has many amazing features, one of which is the ability to track programs using cgroups (by running systemctl status). While awesome for a pid 1 process to do, it is also extremely useful for users, and having it set up and initialize user programs, all the while tracking what is in each cgroup is even more amazing.

All of your systemd user units will go to $HOME/.config/systemd/user. These units take precedence over units in other systemd unit directories.

There are two packages you need to get this working, both currently available from the AUR: xorg-launch-helperAUR and optionally, user-session-unitsAUR if you want to have autologin working.

Next is setting up your targets. You should set up two, one for window manager and another as a default target. The window manager target should be populated like so:

$HOME/.config/systemd/user/wm.target
[Unit]
Description=Window manager target
Wants=xorg.target
Wants=mystuff.target
Requires=dbus.socket
AllowIsolate=true

[Install]
Alias=default.target

This will be the target for your graphical interface.

Put together a second target called mystuff.target. All services but your window manager should contain a WantedBy line, under [Install], pointing at this unit.

$HOME/.config/systemd/user/mystuff.target
[Unit]
Description=Xinitrc Stuff
Wants=wm.target

[Install]
Alias=default.target

Link this unit to default.target. When you start systemd --user, it will start this target.

Next you need to begin writing services. First you should throw together a service for your window manager:

$HOME/.config/systemd/user/YOUR_WM.service
[Unit]
Description=your window manager service
Before=mystuff.target
After=xorg.target
Requires=xorg.target

[Service]
#Environment=PATH=uncomment:to:override:your:PATH
ExecStart=/full/path/to/wm/executable
Restart=always
RestartSec=10
 
[Install]
WantedBy=wm.target
Note: The [Install] section includes a 'WantedBy' part. When using systemctl --user enable it will link this as $HOME/.config/systemd/user/wm.target.wants/i3.service, allowing it to be started at login. Is recommended enabling this service, not linking it manually.

You can fill your user unit directory with a plethora of services, including ones for mpd, gpg-agent, offlineimap, parcellite, pulse, tmux, urxvtd, xbindkeys and xmodmap to name a few.

Auto-login

If you want to have systemd automatically log you in on boot, then you can use the unit in user-session-units to do so. Enabling a screen locker for will stop someone from booting your computer into a nice, logged in session.

If you installed user-session-units as listed above, then you must copy /usr/lib/systemd/system/user-session@.service to /etc/systemd/system/user-session@yourloginname.service) and edit these lines:

Environment=XDG_RUNTIME_DIR=/run/user/%I
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%I/dbus/user_bus_socket

to this:

Environment=XDG_RUNTIME_DIR=/run/user/%U
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%U/dbus/user_bus_socket
Note: Notice the subtle change where %I is replaced by %U

As well as an install section:

[Install]
WantedBy=graphical.target

or if you have no login manager:

[Install]
WantedBy=getty.target

Add this line to /etc/pam.d/login and /etc/pam.d/system-auth:

session    required    pam_systemd.so

Because user-session@.service starts on tty1, you will need to add Conflicts=getty@tty1.service to the service file, if it doesn't exist already.

Once this is done, systemctl --user enable YOUR_WM.service

One of the most important things you can add to the service files you will be writing is the use of Before= and After= in the [Unit] section. These two parts will determine the order things are started. Say you have a graphical application you want to start on boot, you would put After=xorg.target into your unit. Say you start ncmpcpp, which requires mpd to start, you can put After=mpd.service into your ncmpcpp unit. You will eventually figure out exactly how this needs to go either from experience or from reading the systemd manual pages. Starting with systemd.unit(5) is a good idea.

Other use cases

Persistent terminal multiplexer

You may wish your user session to default to running a terminal multiplexer, such as GNU Screen or Tmux, in the background rather than logging you in to a window manager session. Separating login from X login is most likely only useful for those who boot to a TTY instead of to a display manager (in which case you can simply bundle everything you start in with myStuff.target).

To create this type of user session, procede as above, but instead of creating wm.target, create multiplexer.target:

[Unit]
Description=Terminal multiplexer
Documentation=info:screen man:screen(1) man:tmux(1)
After=cruft.target
Wants=cruft.target

[Install]
Alias=default.target

cruft.target, like mystuff.target above, should start anything you think should run before tmux or screen starts (or which you want started at boot regardless of timing), such as a GnuPG daemon session.

You then need to create a service for your multiplexer session. Here's a sample service, using tmux as an example and sourcing a gpg-agent session which wrote its information to /tmp/gpg-agent-info. This sample session, when you start X, will also be able to run X programs, since DISPLAY is set.

[Unit]
Description=tmux: A terminal multiplixer Documentation=man:tmux(1)
After=gpg-agent.service
Wants=gpg-agent.service

[Service]
Type=forking
ExecStart=/usr/bin/tmux start
ExecStop=/usr/bin/tmux kill-server
Environment=DISPLAY=:0
EnvironmentFile=/tmp/gpg-agent-info

[Install]
WantedBy=multiplexer.target

Once this is done, systemctl --user enable tmux.service, multiplexer.target and any services you created to be run by cruft.target and you should be set to go! Activated user-session@.service as described above, but be sure to remove the Conflicts=getty@tty1.service from user-session@.service, since your user session will not be taking over a TTY. Congratulations! You have a running terminal multiplexer and some other useful programs ready to start at boot!

Starting X

You've probably noticed that, since the terminal multiplexer is now default.target, X will not start automatically at boot. To start X, procede as above, but do not activate or manually link to default.target wm.target. Instead, assuming you're booting to a terminal, we'll simply be using a hackish workaround and masking /usr/bin/startx with a shell alias:

alias startx='systemctl --user start wm.target'

User Services

Users may now interact with units located in the following directories just as they would with system services (ordered by ascending precedence):

  • /usr/lib/systemd/user/
  • /etc/systemd/user/
  • ~/.config/systemd/user/

To control the systemd instance, the user must use the command systemctl --user.

Installed by packages

A unit installed by a package that is meant to be run by a systemd user instance should install the unit to /usr/lib/systemd/user/. The system adminstration can then modify the unit by copying it to /etc/systemd/user/. A user can then modify the unit by copying it to ~/.config/systemd/user/.

Example

The following is an example of a user version of the mpd service.

mpd.service
[Unit]
Description=Music Player Daemon

[Service]
ExecStart=/usr/bin/mpd --no-daemon

[Install]
WantedBy=default.target

Example with variables

The following is an example of a user version of sickbeard.service, which takes into account variable home directories where SickBeard can find certain files:

sickbeard.service
[Unit]
Description=SickBeard Daemon

[Service]
ExecStart=/usr/bin/env python2 /opt/sickbeard/SickBeard.py --config %h/.sickbeard/config.ini --datadir %h/.sickbeard

[Install]
WantedBy=default.target

As detailed in man systemd.unit, the %h variable is replaced by the home directory of the user running the service. There are other variables that can be taken into account in the systemd manpages.

Note about X applications

Most X apps, need a DISPLAY variable to run (so it's likely the first reason why your service files aren't starting), so you have to make sure to include it:

$HOME/.config/systemd/user/parcellite.service
[Unit]
Description=Parcellite clipboard manager

[Service]
ExecStart=/usr/bin/parcellite
Environment=DISPLAY=:0 # <= !

[Install]
WantedBy=mystuff.target

A cleaner way though, it's not hard code the DISPLAY environment variable (specially if you run more than on display):

$HOME/.config/systemd/user/x-app-template@.service
[Unit]
Description=Your amazing and original description

[Service]
ExecStart=/full/path/to/the/app
Environment=DISPLAY=%i # <= !

[Install]
WantedBy=mystuff.target

Then you can run it with:

systemctl --user {start|enable} x-app@your-desired-display.service # <= :0 in most cases

See also