Difference between revisions of "Systemd mythbackend Configuration"

From MythTV Official Wiki
Jump to: navigation, search
(Default configuration)
m (Another example for an ubuntu configuration)
(14 intermediate revisions by 9 users not shown)
Line 1: Line 1:
Some newer Linux distributions replace the traditional LSB init script startup management process with a program called systemd, which does things differently; here are some notes on how to make MythTV play nicely with systemd.  A  [https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet cheatsheet] from the traditional init scripts to systemd is available on the [https://fedoraproject.org/wiki Fedora wiki pages.]
+
Some newer Linux distributions (including Ubuntu 15.04 onwards) replace the traditional LSB init script startup management process with a program called systemd, which does things differently; here are some notes on how to make MythTV play nicely with systemd.  A  [https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet cheatsheet] from the traditional init scripts to systemd is available on the [https://fedoraproject.org/wiki Fedora wiki pages.]
  
 
Note that much of the example startup code that follows is based on the rpmfusion packaging of MythTV by Richard Shaw.
 
Note that much of the example startup code that follows is based on the rpmfusion packaging of MythTV by Richard Shaw.
Line 36: Line 36:
 
[Unit]
 
[Unit]
 
Description=MythTV backend service
 
Description=MythTV backend service
After=network.target mysqld.service
+
After=network.target mysqld.service NetworkManager-wait-online.service pingnetwork.service
 +
Wants=pingnetwork.service
 +
 
 
# Uncomment the following line if you will be using the mythweb plugin on the  
 
# Uncomment the following line if you will be using the mythweb plugin on the  
 
# same system as mythbackend.
 
# same system as mythbackend.
Line 45: Line 47:
 
# These require ExecStart lines which are NOT interchangeable.
 
# These require ExecStart lines which are NOT interchangeable.
 
# Type=simple is recommended.  
 
# Type=simple is recommended.  
# If using 'Type=simple' you cannot have --daemon in the command line  
+
# If using 'Type=simple'
 +
# 1) you cannot have --daemon in the command line  
  
# If using 'Type=forking' you must have --daemon in the command line AND
+
# If using 'Type=forking'
# you must have a --pidfile /somewhere so systemd knows how to find the
+
# 1) you must have --daemon in the command line AND
# forked process PID, otherwise it presumes that the process being started
+
# 2) you must have --pidfile /somewhere in the command line AND
# will be the sole remaining process.
+
# 3) you must have a line PIDFILE=/somewhere in the [service] stanza
 +
# this lets systemd know how to find the forked process PID,  
 +
# otherwise it presumes the starting process will be the remaining process
 +
# (cf. with the mysqld_safe process which starts mysqld and then disappears).
 
# Specifying a pidfile with Type simple does not cause any problems and
 
# Specifying a pidfile with Type simple does not cause any problems and
# may help in debugging startup failures
+
# may help in debugging startup failures.
  
 
#Type=forking
 
#Type=forking
Line 74: Line 80:
 
#ExecStart=/usr/bin/mythbackend --syslog local7  
 
#ExecStart=/usr/bin/mythbackend --syslog local7  
  
# If Type=forking is set you must specify the --daemon and --pidfile options:
+
# If Type=forking you must specify the --daemon and --pidfile options:
#ExecStart=/usr/bin/mythbackend --daemon --logpath /var/log/mythtv --loglevel crit --pidfile /run/mythbackend.pid  
+
#ExecStart=/usr/bin/mythbackend --daemon --logpath /var/log/mythtv --loglevel crit --pidfile /run/mythtv/mythbackend.pid
 +
#PIDFILE=/run/mythtv/mythbackend.pid
  
 +
# Systemd sends stdout to syslog, and mythbackend has console logging enabled without --daemon.
 +
# Send stdout to /dev/null so console logging doesn't go to syslog
 +
StandardOutput=null
 
# Default setting for Type=simple.
 
# Default setting for Type=simple.
ExecStart=/usr/bin/mythbackend --logpath /var/log/mythtv
+
ExecStart=/usr/bin/mythbackend --logpath /var/log/mythtv --loglevel crit
  
 
# Whether systemd will continue over-watch and restart on any halt
 
# Whether systemd will continue over-watch and restart on any halt
Line 88: Line 98:
 
</pre>
 
</pre>
 
}}
 
}}
 +
  
 
{{note box|Starting with 0.25, there has been a change in the logging. You must adjust the ExecStart line above based on the active release
 
{{note box|Starting with 0.25, there has been a change in the logging. You must adjust the ExecStart line above based on the active release
Line 107: Line 118:
 
fi
 
fi
 
# make pid folder
 
# make pid folder
mkdir -p -m 755 /var/run/mythtv
+
mkdir -p -m 755 /run/mythtv
chown -hR mythtv:mythtv /var/run/mythtv
+
chown -hR mythtv:mythtv /run/mythtv
 
</pre>
 
</pre>
  
If you have any problems with /var/run/mythtv not being created, try using /var/log/mythtv as the pidfile folder. Remember to check
+
If you have any problems with /run/mythtv not being created, try using /var/log/mythtv as the pidfile folder. Remember to check
 
the ownership and permissions of any tuner sockets in /dev such as /dev/dvb or /dev/video. The mythtv user must be able to access them.}}
 
the ownership and permissions of any tuner sockets in /dev such as /dev/dvb or /dev/video. The mythtv user must be able to access them.}}
 +
==Another example for an ubuntu configuration==
 +
 +
Please check this file is suitable for your requirements and edit where necessary.
 +
 +
Save the configuration file below in <code>/etc/systemd/system</code>.
 +
If you have been using mythbuntu check that there are no mythtv-backend.service files
 +
here or in <code>/lib/systemd/system</code>.
 +
{{Code box|/etc/systemd/system/mythbackend.service|
 +
<pre>
 +
[Unit]
 +
Description=MythTV backend service
 +
# The following line will cause mythbackend to be terminated is the database dies.
 +
# If you do not want this then use a weaker binding such as "Requires" or "Wants".
 +
BindsTo=mysql.service
 +
Wants=network-online.target
 +
After=mysql.service network-online.target
 +
 +
# Persistently named adapters
 +
# Needs a systemd tag added to your udev persistent naming rules
 +
#Requires=dev-dvb-adapter0-frontend0.device
 +
#After=dev-dvb-adapter0-frontend0.device
 +
 +
[Service]
 +
Type=simple
 +
 +
Environment=MYTHCONFDIR=/etc/mythtv
 +
 +
# Allow core dumps
 +
# The core resource limit on the user specified below can reduce what
 +
# is specified here. So it needs to be set as well.
 +
LimitCORE=infinity
 +
 +
User=mythtv
 +
 +
StandardOutput=null
 +
StandardError=journal
 +
 +
ExecStart=/full/path/to/mythbackend --logpath=/full/path/to/logs
 +
 +
[Install]
 +
WantedBy=multi-user.target
 +
</pre>
 +
}}
 +
 +
==Allow the backend to restart on failure==
 +
 +
Sometimes the backend will coredump in the middle of a firewire recording, for example. Or it just gets tired after a while and coredumps. Restarting it can clear things up, and pick up a recording where it left off (minus restart time). ''If'' you're there to restart it. SystemD can restart the backend with a simple setting and manual backend restart.
 +
 +
{{Code box|/etc/systemd/system/mythbackend.service|
 +
<pre>
 +
# Whether systemd will continue over-watch and restart on any halt
 +
Restart=on-failure
 +
</pre>
 +
}}
  
 
==Delay starting the backend until tuners have initialized==
 
==Delay starting the backend until tuners have initialized==
Line 120: Line 185:
 
This can be accomplished by adding additional Wants= and After= stanza to the unit file, but to have systemd create device units, you must first add a rule to the udev rules directory.
 
This can be accomplished by adding additional Wants= and After= stanza to the unit file, but to have systemd create device units, you must first add a rule to the udev rules directory.
  
{{Code box|/etc/udev/rules.d/99-mythbackend|
+
{{Code box|/etc/udev/rules.d/99-mythbackend.rules|
 
<pre>
 
<pre>
 
#
 
#
Line 182: Line 247:
 
Richard
 
Richard
  
 +
==Delay starting the backend until network has initialized==
 +
<pre>
 +
The addition of pingnetwork.service to both a "Wants=" and "After="
 +
line in the [Unit] section of mythbackend.service should greatly
 +
improve/eliminate mythfrontend clients failure to connect to the
 +
server.
 +
 +
As user "root", enable the service: systemctl enable pingnetwork.service
 +
 +
Reference    : mythtv ticket #11160
 +
Fedora 18 rpms: NetworkManager-0.9.7.0-12.git20121004
 +
              : systemd-197-1
 +
 +
If remote clients/mythfrontend fail to connect to mythbackend, on the
 +
machine hosting mythbackend, examine /var/log/boot.log for a failed
 +
"Network Manager Wait Online" service, or execute "systemctl status
 +
NetworkManager-wait-online.service" and look for failure due to timeout.
 +
 +
cp -iv /usr/lib/systemd/system/NetworkManager-wait-online.service \
 +
  /etc/systemd/system/
 +
 +
# Edit /etc/systemd/system/NetworkManager-wait-online.service to extend the
 +
# timeout value. Doubling the default to 60 is suggested.
 +
 +
vi /etc/systemd/system/NetworkManager-wait-online.service
 +
 +
ExecStart=/usr/bin/nm-online -q --timeout=60
 +
 +
Reboot and check boot.log or use systemctl command above to check for the
 +
successful execution of NetworkManager-wait-online.service. Increase the
 +
timeout value if necessary and reboot.
 +
 +
Below is a status report of success:       
 +
 +
systemctl status NetworkManager-wait-online.service
 +
NetworkManager-wait-online.service - Network Manager Wait Online
 +
          Loaded: loaded (/etc/systemd/system/NetworkManager-wait-online.service; enabled)
 +
          Active: inactive (dead) since Tue 2013-02-02 15:31:44 EST; 2min 16s ago
 +
        Process: 827 ExecStart=/usr/bin/nm-online -q --timeout=60 (code=exited, status=0/SUCCESS)
 +
 +
Feb 02 15:31:11 stumble systemd[1]: Starting Network Manager Wait Online...
 +
Feb 02 15:31:44 stumble systemd[1]: Started Network Manager Wait Online.
 +
</pre>
 +
 +
==Quickly terminate mythbackend==
 +
<pre>
 +
If “systemctl stop mythbackend” takes 90 seconds to complete, the mythbackend
 +
process is being shut down uncleanly. One could add “TimeoutStopSec=5” to the
 +
[Service] section of the mythbackend.service file, but this would still kill
 +
mythbackend abruptly. One reason for smoothly shutting down mythbackend would
 +
be to enable use of a program profiler such as the GNU profiling tool gprof.
 +
 +
One solution:
 +
1) add “--pidfile /var/log/mythtv/mythbackend.pid” to your ExecStart line in
 +
mythbackend.service (if necessary).
 +
 +
2) Change/Add the ExecStop line in the same file's [Service] section to:
 +
ExecStop=/usr/local/bin/mythbackendstop
 +
 +
3) Create /usr/local/bin/mythbackendstop. Make the script executable.
 +
#!/bin/bash
 +
# Custom stop script to execute `kill -15` twice (which mythbackend
 +
# apparently needs to exit cleanly)
 +
KILLIT=`/usr/bin/cat /var/log/mythtv/mythbackend.pid`
 +
/usr/bin/kill -15 $KILLIT;/usr/bin/sleep 2;/usr/bin/kill -15 $KILLIT
 +
 +
Once properly set up, “systemctl stop mythbackend” will terminate mythbackend
 +
in just a few seconds.
 +
</pre>
  
 
[[Category:Startup Scripts]]
 
[[Category:Startup Scripts]]

Revision as of 07:06, 19 May 2017

Some newer Linux distributions (including Ubuntu 15.04 onwards) replace the traditional LSB init script startup management process with a program called systemd, which does things differently; here are some notes on how to make MythTV play nicely with systemd. A cheatsheet from the traditional init scripts to systemd is available on the Fedora wiki pages.

Note that much of the example startup code that follows is based on the rpmfusion packaging of MythTV by Richard Shaw.


Default configuration

Save the configuration file below in /etc/systemd/system/mythbackend.service Changes to files in /lib/systemd/system will be over-written when updated.

Script.png /etc/systemd/system/mythbackend.service


# Do not edit this file, it will be replaced on update
# If you need to make modifications to this unit file first make a copy in # /etc/systemd/system

# This file is part of the packaging of MythTV #
# MythTV is free software; view http://www.mythtv.org for more information.
#
# It is sometimes necessary to wait for a capture device to finish (hot)plug
# initialization before the backend starts.  If this is necessary, 
# create a Wants and After entry for all the capture devices that
# one wishes to wait to be created before the backend starts using the systemd
# device name mangling names (/dev/some/thing turns 
# into dev-some-thing.device).  These normally should  be udev persistent 
# filename rules to avoid confusion.
# See the MythTV wiki for udev persistent filename # discussions found at:
# http://www.mythtv.org/wiki/Device_Filenames_and_udev
#
# In order to insure that systemd will create a device unit for the 
# capture device, one must insure that udev will have the tag "systemd"
# (i.e. add TAG+="systemd" to the udev rule).
#
# The MythTV package ships with a default udev rule file located in: 
# /lib/udev/rules.d/99-mythbackend.rules

[Unit]
Description=MythTV backend service
After=network.target mysqld.service NetworkManager-wait-online.service pingnetwork.service
Wants=pingnetwork.service

# Uncomment the following line if you will be using the mythweb plugin on the 
# same system as mythbackend.
#Wants=httpd.service

[Service]
# You have 2 choices for 'Type': simple or forking 
# These require ExecStart lines which are NOT interchangeable.
# Type=simple is recommended. 
# If using 'Type=simple'
# 1) you cannot have --daemon in the command line 

# If using 'Type=forking'
# 1) you must have --daemon in the command line AND
# 2) you must have  --pidfile /somewhere in the command line AND
# 3) you must have a line PIDFILE=/somewhere in the [service] stanza
# this lets systemd know how to find the forked process PID, 
# otherwise it presumes the starting process will be the remaining process
# (cf. with the mysqld_safe process which starts mysqld and then disappears).
# Specifying a pidfile with Type simple does not cause any problems and
# may help in debugging startup failures.

#Type=forking
Type=simple

Environment=MYTHCONFDIR=/etc/mythtv
# Default setting assumes a system wide install. Otherwise it should be set to 
# the folder containing the .mythtv subfolder containing the config.xml file 
# Such as
Environment=HOME=/usr/share/mythtv
# or
Environment=HOME=/~/.mythtv

# systemd changes execution from the calling user (possibly root) to this user
# if you have permissions problems or obscure errors try logging in as mythtv 
# See notes below
User=mythtv

# Uncomment one of the following...
# Use sysloging rather than separate logging:
#ExecStart=/usr/bin/mythbackend --syslog local7 

# If Type=forking you must specify the --daemon and --pidfile options:
#ExecStart=/usr/bin/mythbackend --daemon --logpath /var/log/mythtv --loglevel crit --pidfile /run/mythtv/mythbackend.pid 
#PIDFILE=/run/mythtv/mythbackend.pid

# Systemd sends stdout to syslog, and mythbackend has console logging enabled without --daemon.
# Send stdout to /dev/null so console logging doesn't go to syslog
StandardOutput=null
# Default setting for Type=simple.
ExecStart=/usr/bin/mythbackend --logpath /var/log/mythtv --loglevel crit

# Whether systemd will continue over-watch and restart on any halt
#Restart=Restart-always

[Install]
WantedBy=multi-user.target


Important.png Note: Starting with 0.25, there has been a change in the logging. You must adjust the ExecStart line above based on the active release and logging preference (in 0.25 and later.) See: Logging for details.

To enable the backend to start at the next boot, issue the command 'systemctl enable mythbackend.service'. To start the backend immediately, issue the command 'systemctl start mythbackend.service'.

If you specify a log file, all logging will be to that file. If you specify a logpath each program will write to distinctly named files: much better for debugging. Loglevel determines what is logged. Log messages at lower levels will be discarded: In descending order: emerg, alert, crit, err, warning, notice, info, debug. Defaults to info.


Important.png Note: The example runs the backend as user mythtv. You will need to insure that your files/directories/capture cards can be accessed by the mythtv user, or run the backend as root (not recommended). The following script snippet creates and sets the ownership and permissions for the logging folders.

# Sanity check on required folders
if [ ! -x /var/log/mythtv ]; then
# make logging folder
mkdir -p -m 755 /var/log/mythtv
chown -hR mythtv:mythtv /var/log/mythtv
fi
# make pid folder
mkdir -p -m 755 /run/mythtv
chown -hR mythtv:mythtv /run/mythtv

If you have any problems with /run/mythtv not being created, try using /var/log/mythtv as the pidfile folder. Remember to check the ownership and permissions of any tuner sockets in /dev such as /dev/dvb or /dev/video. The mythtv user must be able to access them.

Another example for an ubuntu configuration

Please check this file is suitable for your requirements and edit where necessary.

Save the configuration file below in /etc/systemd/system. If you have been using mythbuntu check that there are no mythtv-backend.service files here or in /lib/systemd/system.

Script.png /etc/systemd/system/mythbackend.service

[Unit]
Description=MythTV backend service
# The following line will cause mythbackend to be terminated is the database dies.
# If you do not want this then use a weaker binding such as "Requires" or "Wants".
BindsTo=mysql.service
Wants=network-online.target
After=mysql.service network-online.target

# Persistently named adapters
# Needs a systemd tag added to your udev persistent naming rules
#Requires=dev-dvb-adapter0-frontend0.device
#After=dev-dvb-adapter0-frontend0.device

[Service]
Type=simple

Environment=MYTHCONFDIR=/etc/mythtv

# Allow core dumps
# The core resource limit on the user specified below can reduce what
# is specified here. So it needs to be set as well.
LimitCORE=infinity

User=mythtv

StandardOutput=null
StandardError=journal

ExecStart=/full/path/to/mythbackend --logpath=/full/path/to/logs

[Install]
WantedBy=multi-user.target

Allow the backend to restart on failure

Sometimes the backend will coredump in the middle of a firewire recording, for example. Or it just gets tired after a while and coredumps. Restarting it can clear things up, and pick up a recording where it left off (minus restart time). If you're there to restart it. SystemD can restart the backend with a simple setting and manual backend restart.


Script.png /etc/systemd/system/mythbackend.service

# Whether systemd will continue over-watch and restart on any halt
Restart=on-failure

Delay starting the backend until tuners have initialized

Some tuners take a long time to initialize (typically, firmware loading) and may therefore not yet be available when the backend starts. Since the backend checks for the presence of tuners upon startup, tuner initialization needs to be completed before the backend is started.

This can be accomplished by adding additional Wants= and After= stanza to the unit file, but to have systemd create device units, you must first add a rule to the udev rules directory.


Script.png /etc/udev/rules.d/99-mythbackend.rules

#
# Create systemd device units for capture devices
#
SUBSYSTEM=="video4linux", TAG+="systemd"
SUBSYSTEM=="dvb", TAG+="systemd"
SUBSYSTEM=="firewire", TAG+="systemd"


With the udev rule tag, systemd will create a device unit at startup that one can add to the [Unit] stanza in the startup. Note that one must use the systemd mangled names (generally /dev/some/thing is mangled into dev-some-thing.device). It is highly recommended that you use udev persistent names rather than base names such as /dev/video0.


Script.png device wait code for a typical v4l2 device


Wants=dev-video300.device
After=dev-video300.device


Script.png device wait code for a typical dvb device


Wants=dev-dvb-adapter200-frontend0.device
After=dev-dvb-adapter200-frontend0.device

From mythtv-users post from Richard Shaw

Systemd has a built-in timeout on device units so that startup will not wait forever for a failed device startup.

There are many options for how to run mythbackend with SystemD but one decision you

  • MUST*
  • MUST*
  • MUST*

make is if you're going to run:

Type=simple

or

Type=forking

If you use Type=simple (which is my recommendation) then you *CANNOT* use the "--daemon" option because SystemD is not expecting the daemon to fork!!!

If you use Type=forking then you *MUST* use the "--daemon" option as well as specify a PID file, "--pidfile" in ExecStart *AND* you must set the PIDFile= systemd option so it knows where to find the PID file.

Someone please fix the wiki!

Thanks Richard

Delay starting the backend until network has initialized

The addition of pingnetwork.service to both a "Wants=" and "After="
line in the [Unit] section of mythbackend.service should greatly 
improve/eliminate mythfrontend clients failure to connect to the 
server.

As user "root", enable the service: systemctl enable pingnetwork.service

Reference     : mythtv ticket #11160
Fedora 18 rpms: NetworkManager-0.9.7.0-12.git20121004
              : systemd-197-1

If remote clients/mythfrontend fail to connect to mythbackend, on the 
machine hosting mythbackend, examine /var/log/boot.log for a failed 
"Network Manager Wait Online" service, or execute "systemctl status 
NetworkManager-wait-online.service" and look for failure due to timeout.

cp -iv /usr/lib/systemd/system/NetworkManager-wait-online.service \
  /etc/systemd/system/

# Edit /etc/systemd/system/NetworkManager-wait-online.service to extend the
# timeout value. Doubling the default to 60 is suggested.

vi /etc/systemd/system/NetworkManager-wait-online.service

ExecStart=/usr/bin/nm-online -q --timeout=60

Reboot and check boot.log or use systemctl command above to check for the
successful execution of NetworkManager-wait-online.service. Increase the
timeout value if necessary and reboot.

Below is a status report of success:         
 
systemctl status NetworkManager-wait-online.service
NetworkManager-wait-online.service - Network Manager Wait Online
          Loaded: loaded (/etc/systemd/system/NetworkManager-wait-online.service; enabled)
          Active: inactive (dead) since Tue 2013-02-02 15:31:44 EST; 2min 16s ago
         Process: 827 ExecStart=/usr/bin/nm-online -q --timeout=60 (code=exited, status=0/SUCCESS)

Feb 02 15:31:11 stumble systemd[1]: Starting Network Manager Wait Online...
Feb 02 15:31:44 stumble systemd[1]: Started Network Manager Wait Online.

Quickly terminate mythbackend

If “systemctl stop mythbackend” takes 90 seconds to complete, the mythbackend
process is being shut down uncleanly. One could add “TimeoutStopSec=5” to the
[Service] section of the mythbackend.service file, but this would still kill
mythbackend abruptly. One reason for smoothly shutting down mythbackend would
be to enable use of a program profiler such as the GNU profiling tool gprof.

One solution:
1) add “--pidfile /var/log/mythtv/mythbackend.pid” to your ExecStart line in 
mythbackend.service (if necessary).

2) Change/Add the ExecStop line in the same file's [Service] section to:
ExecStop=/usr/local/bin/mythbackendstop

3) Create /usr/local/bin/mythbackendstop. Make the script executable.
#!/bin/bash 
# Custom stop script to execute `kill -15` twice (which mythbackend
# apparently needs to exit cleanly)
KILLIT=`/usr/bin/cat /var/log/mythtv/mythbackend.pid` 
/usr/bin/kill -15 $KILLIT;/usr/bin/sleep 2;/usr/bin/kill -15 $KILLIT

Once properly set up, “systemctl stop mythbackend” will terminate mythbackend
in just a few seconds.