How to start XBMC (kodi) with systemd without login manager

Long story short: My HTPC is running on unstable/experimental branch of Debian GNU/Linux OS and I decided to update it again.
It ended with this message xf86OpenConsole: Cannot open virtual console 7 when XBMC had to start via init script (by invoking xinit with user privs).
Upon long examination I decided to rewrite my configuration in systemd way.

I made two new services which I placed to /etc/systemd/system named xorg.service and xbmc.service
Their content is described bellow.

xorg.service
 
[Unit]
Description=Launch Xorg server
After=network.target sound.target
 
[Service]
Type=simple
ExecStart=/usr/bin/X :0 vt7
 
[Install]
WantedBy=multi-user.target
 
This service starts Xorg server after network and sound services when system is entering to multi-user level.

xbmc.service
 
[Unit]
Description=Launch XBMC
After=xorg.service
Requires=xorg.service
 
[Service]
Type=simple
User=xbmc
Group=xbmc
Environment="DISPLAY=:0"
ExecStart=/usr/bin/kodi-standalone
KillSignal=SIGTERM
SendSIGHUP=1
Restart=on-abort
RestartSec=5
 
[Install]
WantedBy=multi-user.target
 
This service starts XBMC but only when Xorg service is running (note the Requires option).
XBMC is started with privileges of xbmc user and group which are present on my system.
There is also environment variable $DISPLAY set to :0 (which is display identifier).
These options are pretty self-describing but I have to note the SendSIGHUP which is interesting one,
because when this option is set, systemd will send SIGHUP signal to all remaining forks,
so they will exit quickly when main process isn't present. This solved slow XBMC shutdowns for me within this setup.

Ensure that services will start with system.
systemctl daemon-reload
systemctl enable xorg
systemctl enable xbmc

... and finaly
systemctl xbmc start

In summary I have XBMC running without root privileges in pure and clean X session without any tools which can be easily broken in such experimental environment.

author: niekto@niekde.sk (Jaroslav Petráš)

date: Sat, 03 Oct 2015 14:55:00 +0000

link: CyberAsylum.eu/how-to-start-xbmc-via-systemd-without-login-manager