https://wiki.archlinux.org/api.php?action=feedcontributions&user=Luk&feedformat=atomArchWiki - User contributions [en]2024-03-29T05:26:52ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Systemd/User&diff=259069Systemd/User2013-05-27T09:24:17Z<p>Luk: /* See also */</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services]]<br />
[[Category:Boot process]]<br />
[[es:Systemd/User]]<br />
[[it:Systemd/User]]<br />
{{Article summary start}}<br />
{{Article summary text|Covers how to set up [[systemd]] user sessions.}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|systemd}}<br />
{{Article summary end}}<br />
<br />
[[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]].<br />
<br />
{{Note|/usr/lib/systemd/systemd --user sessions are not compatible with -ck patchset}}<br />
<br />
== Setup ==<br />
<br />
=== startx ===<br />
<br />
{{Note|This step is unnecessary if you plan to use autologin.}}<br />
<br />
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.<br />
<br />
Next, the user must launch systemd by putting the following in their {{ic|~/.xinitrc}}.<br />
{{bc|/usr/lib/systemd/systemd --user}}<br />
If the user is not launching the window manager through /usr/lib/systemd/systemd --user, then {{bc|/usr/lib/systemd/systemd --user &}} should be used and launched like anything else in {{ic|~/.xinitrc}}, before execing the window manager.<br />
<br />
After starting X, the user can check whether their session is now being managed by systemd-logind with the following command:<br />
<br />
{{bc|<nowiki><br />
$ loginctl --no-pager show-session $XDG_SESSION_ID | grep Active<br />
</nowiki>}}<br />
<br />
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.<br />
<br />
=== Display Managers ===<br />
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|/usr/lib/systemd/systemd --user}} as a program to be started by their desktop environment.<br />
<br />
==== GNOME 3 (using GDM) ====<br />
For users who wish to have GDM/GNOME 3 auto-start their {{ic|/usr/lib/systemd/systemd --user}} session upon login, they just need to add a special log in session for this:<br />
{{hc|/usr/share/xsessions/gnome-systemd.desktop|<nowiki><br />
[Desktop Entry]<br />
Type=Application<br />
Name=systemd<br />
Comment=Runs 'systemd' as a user instance.<br />
Exec=/usr/lib/systemd/systemd --user<br />
</nowiki>}}<br />
<br />
Make sure to choose the {{ic|systemd}} session option at the GDM login screen.<br />
{{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.}}<br />
<br />
=== Using /usr/lib/systemd/systemd --user To Manage Your Session ===<br />
<br />
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.<br />
<br />
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.<br />
<br />
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. <br />
<br />
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:<br />
<br />
{{hc|$HOME/.config/systemd/user/wm.target|<nowiki><br />
[Unit]<br />
Description=Window manager target<br />
Wants=xorg.target<br />
Wants=mystuff.target<br />
Requires=dbus.socket<br />
AllowIsolate=true<br />
<br />
[Install]<br />
Alias=default.target<br />
</nowiki>}}<br />
This will be the target for your graphical interface.<br />
<br />
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.<br />
<br />
{{hc|$HOME/.config/systemd/user/mystuff.target|<nowiki><br />
[Unit]<br />
Description=Xinitrc Stuff<br />
Wants=wm.target<br />
<br />
[Install]<br />
Alias=default.target<br />
</nowiki>}}<br />
<br />
Link this unit to {{ic|default.target}}. When you start {{ic|/usr/lib/systemd/systemd --user}}, it will start this target. <br />
<br />
Next you need to begin writing services. First you should throw together a service for your window manager:<br />
<br />
{{hc|$HOME/.config/systemd/user/YOUR_WM.service|<nowiki><br />
[Unit]<br />
Description=your window manager service<br />
Before=mystuff.target<br />
After=xorg.target<br />
Requires=xorg.target<br />
<br />
[Service]<br />
#Environment=PATH=uncomment:to:override:your:PATH<br />
ExecStart=/full/path/to/wm/executable<br />
Restart=always<br />
RestartSec=10<br />
<br />
[Install]<br />
WantedBy=wm.target<br />
</nowiki>}}<br />
<br />
{{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.}}<br />
<br />
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.<br />
<br />
===Auto-login===<br />
<br />
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.<br />
Add this line to {{ic|/etc/pam.d/login}} and {{ic|/etc/pam.d/system-auth}}:<br />
{{bc|session required pam_systemd.so}}<br />
<br />
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. Alternately, you can have it run on tty7 instead by modifying {{ic|TTYPath}} accordingly as well as the {{ic|ExecStart}} line in {{ic|xorg.service}} ({{ic|cp /usr/lib/systemd/user/xorg.service /etc/systemd/user/}} and make the modifications there).<br />
<br />
Once this is done, {{ic|systemctl --user enable}} {{ic|YOUR_WM.service}}<br />
<br />
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.<br />
<br />
=== Other use cases ===<br />
<br />
==== Persistent terminal multiplexer ====<br />
<br />
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 into 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). <br />
<br />
To create this type of user session, procede as above, but instead of creating wm.target, create multiplexer.target:<br />
<br />
{{bc|1=<br />
[Unit]<br />
Description=Terminal multiplexer<br />
Documentation=info:screen man:screen(1) man:tmux(1)<br />
After=cruft.target<br />
Wants=cruft.target<br />
<br />
[Install]<br />
Alias=default.target<br />
}}<br />
<br />
{{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.<br />
<br />
You then need to create a service for your multiplexer session. Here is a sample service, using tmux as an example and sourcing a gpg-agent session which wrote its information to {{ic|/tmp/gpg-agent-info}}. This sample session, when you start X, will also be able to run X programs, since DISPLAY is set.<br />
<br />
{{bc|1=<br />
[Unit]<br />
Description=tmux: A terminal multiplixer Documentation=man:tmux(1)<br />
After=gpg-agent.service<br />
Wants=gpg-agent.service<br />
<br />
[Service]<br />
Type=forking<br />
ExecStart=/usr/bin/tmux start<br />
ExecStop=/usr/bin/tmux kill-server<br />
Environment=DISPLAY=:0<br />
EnvironmentFile=/tmp/gpg-agent-info<br />
<br />
[Install]<br />
WantedBy=multiplexer.target<br />
}}<br />
<br />
Once this is done, {{ic|/usr/lib/systemd/systemd --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!<br />
<br />
===== Starting X =====<br />
<br />
You have probably noticed that, since the terminal multiplexer is now {{ic|default.target}}, X will not start automatically at boot. To start X, proceed as above, but do not activate or manually link to {{ic|default.target}} {{ic|wm.target}}. Instead, assuming you are booting to a terminal, we will simply be using a hack-ish workaround and masking {{ic|/usr/bin/startx}} with a shell alias:<br />
<br />
{{bc|1=alias startx='/usr/lib/systemd/systemd --user start wm.target'}}<br />
<br />
== User Services ==<br />
Users may now interact with units located in the following directories just as they would with system services (ordered by ascending precedence):<br />
* {{ic|/usr/lib/systemd/user/}}<br />
* {{ic|/etc/systemd/user/}}<br />
* {{ic|~/.config/systemd/user/}}<br />
To control the systemd instance, the user must use the command {{ic|/usr/lib/systemd/systemd --user}}.<br />
=== Installed by packages ===<br />
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/}}.<br />
=== Example ===<br />
The following is an example of a user version of the mpd service.<br />
{{hc|mpd.service|<nowiki><br />
[Unit]<br />
Description=Music Player Daemon<br />
<br />
[Service]<br />
ExecStart=/usr/bin/mpd --no-daemon<br />
<br />
[Install]<br />
WantedBy=default.target<br />
</nowiki>}}<br />
=== Example with variables ===<br />
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:<br />
{{hc|sickbeard.service|<nowiki><br />
[Unit]<br />
Description=SickBeard Daemon<br />
<br />
[Service]<br />
ExecStart=/usr/bin/env python2 /opt/sickbeard/SickBeard.py --config %h/.sickbeard/config.ini --datadir %h/.sickbeard<br />
<br />
[Install]<br />
WantedBy=default.target<br />
</nowiki>}}<br />
<br />
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.<br />
<br />
=== Note about X applications ===<br />
<br />
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:<br />
<br />
{{hc|$HOME/.config/systemd/user/parcellite.service|<nowiki><br />
[Unit]<br />
Description=Parcellite clipboard manager<br />
<br />
[Service]<br />
ExecStart=/usr/bin/parcellite<br />
Environment=DISPLAY=:0 # <= !<br />
<br />
[Install]<br />
WantedBy=mystuff.target<br />
</nowiki>}}<br />
<br />
A simpler way, if using {{AUR|user-session-units}}, is to define it in {{ic|user-session@yourloginname.service}} so it's inherited. Add {{ic|1=Environment=DISPLAY=:0}} to the {{ic|[Service]}} section. Another helpful environment variable to set here is {{ic|SHELL}}.<br />
<br />
A cleaner way though, is to '''not''' hard code the DISPLAY environment variable (specially if you run more than on display):<br />
<br />
{{hc|$HOME/.config/systemd/user/x-app-template@.service|<nowiki><br />
[Unit]<br />
Description=Your amazing and original description<br />
<br />
[Service]<br />
ExecStart=/full/path/to/the/app<br />
Environment=DISPLAY=%i # <= !<br />
<br />
[Install]<br />
WantedBy=mystuff.target<br />
</nowiki>}}<br />
<br />
Then you can run it with:<br />
<br />
/usr/lib/systemd/systemd --user {start|enable} x-app@your-desired-display.service # <= :0 in most cases<br />
<br />
== See also ==<br />
* [http://blog.gtmanfred.com/?p=26 gtmanfred's guide - the original guide]{{Linkrot|2013|04|12}}<br />
* [https://bitbucket.org/KaiSforza/systemd-user-units/wiki/Home KaiSforza's bitbucket wiki]<br />
* [https://github.com/zoqaeski/systemd-user-units/wiki/home zoqaeski's github wiki]<br />
* [https://github.com/grawity/systemd-user-units Collection of useful systemd user units]<br />
* [https://bitbucket.org/KaiSforza/systemd-user-units More systemd user units]</div>Lukhttps://wiki.archlinux.org/index.php?title=Systemd/User&diff=259065Systemd/User2013-05-27T08:37:47Z<p>Luk: /* Using /usr/lib/systemd/systemd --user To Manage Your Session */</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services]]<br />
[[Category:Boot process]]<br />
[[es:Systemd/User]]<br />
[[it:Systemd/User]]<br />
{{Article summary start}}<br />
{{Article summary text|Covers how to set up [[systemd]] user sessions.}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|systemd}}<br />
{{Article summary end}}<br />
<br />
[[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]].<br />
<br />
{{Note|/usr/lib/systemd/systemd --user sessions are not compatible with -ck patchset}}<br />
<br />
== Setup ==<br />
<br />
=== startx ===<br />
<br />
{{Note|This step is unnecessary if you plan to use autologin.}}<br />
<br />
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.<br />
<br />
Next, the user must launch systemd by putting the following in their {{ic|~/.xinitrc}}.<br />
{{bc|/usr/lib/systemd/systemd --user}}<br />
If the user is not launching the window manager through /usr/lib/systemd/systemd --user, then {{bc|/usr/lib/systemd/systemd --user &}} should be used and launched like anything else in {{ic|~/.xinitrc}}, before execing the window manager.<br />
<br />
After starting X, the user can check whether their session is now being managed by systemd-logind with the following command:<br />
<br />
{{bc|<nowiki><br />
$ loginctl --no-pager show-session $XDG_SESSION_ID | grep Active<br />
</nowiki>}}<br />
<br />
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.<br />
<br />
=== Display Managers ===<br />
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|/usr/lib/systemd/systemd --user}} as a program to be started by their desktop environment.<br />
<br />
==== GNOME 3 (using GDM) ====<br />
For users who wish to have GDM/GNOME 3 auto-start their {{ic|/usr/lib/systemd/systemd --user}} session upon login, they just need to add a special log in session for this:<br />
{{hc|/usr/share/xsessions/gnome-systemd.desktop|<nowiki><br />
[Desktop Entry]<br />
Type=Application<br />
Name=systemd<br />
Comment=Runs 'systemd' as a user instance.<br />
Exec=/usr/lib/systemd/systemd --user<br />
</nowiki>}}<br />
<br />
Make sure to choose the {{ic|systemd}} session option at the GDM login screen.<br />
{{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.}}<br />
<br />
=== Using /usr/lib/systemd/systemd --user To Manage Your Session ===<br />
<br />
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.<br />
<br />
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.<br />
<br />
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. <br />
<br />
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:<br />
<br />
{{hc|$HOME/.config/systemd/user/wm.target|<nowiki><br />
[Unit]<br />
Description=Window manager target<br />
Wants=xorg.target<br />
Wants=mystuff.target<br />
Requires=dbus.socket<br />
AllowIsolate=true<br />
<br />
[Install]<br />
Alias=default.target<br />
</nowiki>}}<br />
This will be the target for your graphical interface.<br />
<br />
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.<br />
<br />
{{hc|$HOME/.config/systemd/user/mystuff.target|<nowiki><br />
[Unit]<br />
Description=Xinitrc Stuff<br />
Wants=wm.target<br />
<br />
[Install]<br />
Alias=default.target<br />
</nowiki>}}<br />
<br />
Link this unit to {{ic|default.target}}. When you start {{ic|/usr/lib/systemd/systemd --user}}, it will start this target. <br />
<br />
Next you need to begin writing services. First you should throw together a service for your window manager:<br />
<br />
{{hc|$HOME/.config/systemd/user/YOUR_WM.service|<nowiki><br />
[Unit]<br />
Description=your window manager service<br />
Before=mystuff.target<br />
After=xorg.target<br />
Requires=xorg.target<br />
<br />
[Service]<br />
#Environment=PATH=uncomment:to:override:your:PATH<br />
ExecStart=/full/path/to/wm/executable<br />
Restart=always<br />
RestartSec=10<br />
<br />
[Install]<br />
WantedBy=wm.target<br />
</nowiki>}}<br />
<br />
{{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.}}<br />
<br />
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.<br />
<br />
===Auto-login===<br />
<br />
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.<br />
Add this line to {{ic|/etc/pam.d/login}} and {{ic|/etc/pam.d/system-auth}}:<br />
{{bc|session required pam_systemd.so}}<br />
<br />
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. Alternately, you can have it run on tty7 instead by modifying {{ic|TTYPath}} accordingly as well as the {{ic|ExecStart}} line in {{ic|xorg.service}} ({{ic|cp /usr/lib/systemd/user/xorg.service /etc/systemd/user/}} and make the modifications there).<br />
<br />
Once this is done, {{ic|systemctl --user enable}} {{ic|YOUR_WM.service}}<br />
<br />
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.<br />
<br />
=== Other use cases ===<br />
<br />
==== Persistent terminal multiplexer ====<br />
<br />
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 into 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). <br />
<br />
To create this type of user session, procede as above, but instead of creating wm.target, create multiplexer.target:<br />
<br />
{{bc|1=<br />
[Unit]<br />
Description=Terminal multiplexer<br />
Documentation=info:screen man:screen(1) man:tmux(1)<br />
After=cruft.target<br />
Wants=cruft.target<br />
<br />
[Install]<br />
Alias=default.target<br />
}}<br />
<br />
{{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.<br />
<br />
You then need to create a service for your multiplexer session. Here is a sample service, using tmux as an example and sourcing a gpg-agent session which wrote its information to {{ic|/tmp/gpg-agent-info}}. This sample session, when you start X, will also be able to run X programs, since DISPLAY is set.<br />
<br />
{{bc|1=<br />
[Unit]<br />
Description=tmux: A terminal multiplixer Documentation=man:tmux(1)<br />
After=gpg-agent.service<br />
Wants=gpg-agent.service<br />
<br />
[Service]<br />
Type=forking<br />
ExecStart=/usr/bin/tmux start<br />
ExecStop=/usr/bin/tmux kill-server<br />
Environment=DISPLAY=:0<br />
EnvironmentFile=/tmp/gpg-agent-info<br />
<br />
[Install]<br />
WantedBy=multiplexer.target<br />
}}<br />
<br />
Once this is done, {{ic|/usr/lib/systemd/systemd --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!<br />
<br />
===== Starting X =====<br />
<br />
You have probably noticed that, since the terminal multiplexer is now {{ic|default.target}}, X will not start automatically at boot. To start X, proceed as above, but do not activate or manually link to {{ic|default.target}} {{ic|wm.target}}. Instead, assuming you are booting to a terminal, we will simply be using a hack-ish workaround and masking {{ic|/usr/bin/startx}} with a shell alias:<br />
<br />
{{bc|1=alias startx='/usr/lib/systemd/systemd --user start wm.target'}}<br />
<br />
== User Services ==<br />
Users may now interact with units located in the following directories just as they would with system services (ordered by ascending precedence):<br />
* {{ic|/usr/lib/systemd/user/}}<br />
* {{ic|/etc/systemd/user/}}<br />
* {{ic|~/.config/systemd/user/}}<br />
To control the systemd instance, the user must use the command {{ic|/usr/lib/systemd/systemd --user}}.<br />
=== Installed by packages ===<br />
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/}}.<br />
=== Example ===<br />
The following is an example of a user version of the mpd service.<br />
{{hc|mpd.service|<nowiki><br />
[Unit]<br />
Description=Music Player Daemon<br />
<br />
[Service]<br />
ExecStart=/usr/bin/mpd --no-daemon<br />
<br />
[Install]<br />
WantedBy=default.target<br />
</nowiki>}}<br />
=== Example with variables ===<br />
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:<br />
{{hc|sickbeard.service|<nowiki><br />
[Unit]<br />
Description=SickBeard Daemon<br />
<br />
[Service]<br />
ExecStart=/usr/bin/env python2 /opt/sickbeard/SickBeard.py --config %h/.sickbeard/config.ini --datadir %h/.sickbeard<br />
<br />
[Install]<br />
WantedBy=default.target<br />
</nowiki>}}<br />
<br />
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.<br />
<br />
=== Note about X applications ===<br />
<br />
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:<br />
<br />
{{hc|$HOME/.config/systemd/user/parcellite.service|<nowiki><br />
[Unit]<br />
Description=Parcellite clipboard manager<br />
<br />
[Service]<br />
ExecStart=/usr/bin/parcellite<br />
Environment=DISPLAY=:0 # <= !<br />
<br />
[Install]<br />
WantedBy=mystuff.target<br />
</nowiki>}}<br />
<br />
A simpler way, if using {{AUR|user-session-units}}, is to define it in {{ic|user-session@yourloginname.service}} so it's inherited. Add {{ic|1=Environment=DISPLAY=:0}} to the {{ic|[Service]}} section. Another helpful environment variable to set here is {{ic|SHELL}}.<br />
<br />
A cleaner way though, is to '''not''' hard code the DISPLAY environment variable (specially if you run more than on display):<br />
<br />
{{hc|$HOME/.config/systemd/user/x-app-template@.service|<nowiki><br />
[Unit]<br />
Description=Your amazing and original description<br />
<br />
[Service]<br />
ExecStart=/full/path/to/the/app<br />
Environment=DISPLAY=%i # <= !<br />
<br />
[Install]<br />
WantedBy=mystuff.target<br />
</nowiki>}}<br />
<br />
Then you can run it with:<br />
<br />
/usr/lib/systemd/systemd --user {start|enable} x-app@your-desired-display.service # <= :0 in most cases<br />
<br />
== See also ==<br />
* [http://blog.gtmanfred.com/?p=26 gtmanfred's guide - the original guide]{{Linkrot|2013|04|12}}<br />
* [https://bitbucket.org/KaiSforza/systemd-user-units/wiki/Home KaiSforza's bitbucket wiki]<br />
* [https://github.com/grawity/systemd-user-units Collection of useful systemd user units]<br />
* [https://bitbucket.org/KaiSforza/systemd-user-units More systemd user units]</div>Luk