You are not logged in.

#1 2012-05-10 16:07:28

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

mythtv with systemd problems [SOLVED]

Anyone using mythtv with systemd?  I am using the following to get systemd to start mythbackend but it attempts to start before mysqld is fully ready and thus fails:

$ cat /etc/systemd/system/mythbackend.service
[Unit]
Description=MythTV backend service
After=network.target mysqld.service

[Service]
Type=simple
Environment=MYTHCONFDIR=/etc/conf.d/mythbackend
Environment=HOME=/usr/share/mythtv
User=mythtv
ExecStart=/usr/bin/mythbackend --logpath /var/log/mythtv

[Install]
WantedBy=multi-user.target

Here is the status output... any advice?

# systemctl status mythbackend.service
mythbackend.service - MythTV backend service
	  Loaded: loaded (/etc/systemd/system/mythbackend.service; enabled)
	  Active: failed (Result: exit-code) since Thu, 10 May 2012 11:59:57 -0400; 2min 40s ago
	 Process: 415 ExecStart=/usr/bin/mythbackend --logpath /var/log/mythtv/mythbackend.log (code=exited, status=130)
	  CGroup: name=systemd:/system/mythbackend.service

May 10 11:59:54 mars mythbackend[415]: 2012-05-10 11:59:54.273466 E  Driver error was [1/2002]:
May 10 11:59:54 mars mythbackend[415]: QMYSQL: Unable to connect
May 10 11:59:54 mars mythbackend[415]: Database error was:
May 10 11:59:54 mars mythbackend[415]: Can't connect to local MySQL server through socket '/var/ru... (2)
May 10 11:59:56 mars mythbackend[415]: No UPnP backends found
May 10 11:59:56 mars mythbackend[415]: Would you like to configure the database connection now? [no]
May 10 11:59:56 mars mythbackend[415]: [console is not interactive, using default 'no']
May 10 11:59:56 mars mythbackend[415]: 2012-05-10 11:59:56.526065 I  UPnPautoconf() - No UPnP back...ound
May 10 11:59:56 mars mythbackend[415]: 2012-05-10 11:59:56.526080 A  No UPnP backends found
May 10 11:59:56 mars mythbackend[415]: 2012-05-10 11:59:56.526190 C  Failed to init MythContext.

Last edited by graysky (2012-05-11 20:17:34)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2012-05-10 17:22:30

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: mythtv with systemd problems [SOLVED]

The problem is that the mysql service file sucks. systemd will just start mysqld but has no way to know when mysql is actually ready.
The rc.d script uses an imho very ugly hack for this. it forks mysqld and then checks if the server is available in 1 second intervals with "mysqladmin ping".

It seems like fedora worked around this with an ExecStartPost script which waits for the server to be available ( see https://bugzilla.redhat.com/show_bug.cgi?id=714426 ). My guess is that this script does something similiar to the rc.d script. This is just as ugly, but I don't see a better way without mysqld supporting systemd.

Offline

#3 2012-05-10 17:40:00

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: mythtv with systemd problems [SOLVED]

Thanks for the link.  You're right, it does suck smile  Perhaps a clearer solution would be an entry in /etc/rc.local like:

sleep 2s && systemctl start mythbackend.service

Guess it would be nice to see the script they're calling: /usr/libexec/mysqld-wait-ready

Last edited by graysky (2012-05-10 17:41:23)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2012-05-10 17:53:50

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: mythtv with systemd problems [SOLVED]

better to not use rc.local, it's not supported by systemd and this way you also can't be sure that mysql is up yet. My suggestion would be to do it like fedora. The script shouldn't be that hard to write, look at the rc.d script, it's just a loop with "mysqladmin ping". Or try to find the mysqld-wait-ready script... wink
Then set ExecStartPost=<path-to-script> in the service file.

I'm lucky that i don't need this because I'm only using MySQL for my XBMC database. smile

Offline

#5 2012-05-10 18:37:27

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: mythtv with systemd problems [SOLVED]

@65kid - seems like Fedora should have their packages online like we do somewhere.  Just curiously, why use mysql at all for xbmc since it supports mysql-lite out-of-the-box?


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#6 2012-05-10 19:01:52

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: mythtv with systemd problems [SOLVED]

graysky wrote:

@65kid - seems like Fedora should have their packages online like we do somewhere.  Just curiously, why use mysql at all for xbmc since it supports mysql-lite out-of-the-box?

I assume you mean sqlite?
I use MySQL because I have XBMC running on multiple machines and this way they all share the same database. I can even stop a video on one machine and continue it on another easily because XBMC writes the "stopped-at-timestamp" into the database. smile

Offline

#7 2012-05-10 19:03:19

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: mythtv with systemd problems [SOLVED]

Cool, 65kid.  Back on topic: http://rpm.pbone.net/index.php3/stat/4/ … 4.rpm.html

$ cat ./usr/libexec/mysqld-wait-ready
#!/bin/sh

# This script waits for mysqld to be ready to accept connections
# (which can be many seconds or even minutes after launch, if there's
# a lot of crash-recovery work to do).
# Running this as ExecStartPost is useful so that services declared as
# "After mysqld" won't be started until the database is really ready.

# Service file passes us the daemon's PID
daemon_pid="$1"

# extract value of a MySQL option from config files
# Usage: get_mysql_option SECTION VARNAME DEFAULT
# result is returned in $result
# We use my_print_defaults which prints all options from multiple files,
# with the more specific ones later; hence take the last match.
get_mysql_option(){
	result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`
	if [ -z "$result" ]; then
	    # not found, use default
	    result="$3"
	fi
}

# Defaults here had better match what mysqld_safe will default to
get_mysql_option mysqld datadir "/var/lib/mysql"
datadir="$result"
get_mysql_option mysqld socket "$datadir/mysql.sock"
socketfile="$result"

# Wait for the server to come up or for the mysqld process to disappear
ret=0
while /bin/true; do
	RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`
	mret=$?
	if [ $mret -eq 0 ]; then
	    break
	fi
	# exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected,
	# anything else suggests a configuration error
	if [ $mret -ne 1 -a $mret -ne 11 ]; then
	    ret=1
	    break
	fi
	# "Access denied" also means the server is alive
	echo "$RESPONSE" | grep -q "Access denied for user" && break

	# Check process still exists
	if ! /bin/kill -0 $daemon_pid 2>/dev/null; then
	    ret=1
	    break
	fi
	sleep 1
done

WTF: /usr/bin/my_print_defaults

Here is the Arch rc.d script... how to adapt this for systemd?

stat_busy "Starting MySQL Server"
    [ ! -d /var/run/mysqld ] && install -d -g mysql -o mysql /var/run/mysqld &>/dev/null
    if [ -z "$(getPID)" ]; then
       /usr/bin/mysqld_safe --user=mysql &>/dev/null &
      if [ $? -gt 0 ]; then
        stat_fail
        exit 1
      else
        timeo=30
        while [ $timeo -gt 0 ]; do
          response=`/usr/bin/mysqladmin -uUNKNOWN_USER ping 2>&1` && break
          echo "$response" | grep -q "mysqld is alive" && break
          sleep 1
          let timeo=${timeo}-1
        done
        if [ $timeo -eq 0 ]; then
          stat_fail
          exit 1
        else
          echo $(getPID) > /var/run/mysqld/mysqld.pid
          add_daemon mysqld
          stat_done
        fi
      fi
    else
      stat_fail
      exit 1
    fi

Last edited by graysky (2012-05-10 19:06:04)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#8 2012-05-10 19:18:50

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: mythtv with systemd problems [SOLVED]

Seems to work for me:

[Service]
...
ExecStartPost=/etc/systemd/system/mysql-wait.sh $MAINPID

these kind of crappy shell scripts are actually what systemd wants to get rid of, let's hope mysql upstream fixes this. sad

Offline

#9 2012-05-10 19:21:18

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: mythtv with systemd problems [SOLVED]

@65kid - you're using the fedora script?  Can you post the output of :

$ pacman -Qo /usr/bin/my_print_defaults

CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#10 2012-05-10 19:23:07

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: mythtv with systemd problems [SOLVED]

graysky wrote:

@65kid - you're using the fedora script?  Can you post the output of :

$ pacman -Qo /usr/bin/my_print_defaults

yes, using the fedora script.

/usr/bin/my_print_defaults is owned by mysql 5.5.24-1

Offline

#11 2012-05-10 20:21:56

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: mythtv with systemd problems [SOLVED]

I'm not using systemd at the moment, but will probably make the switch at some point. Let me know if you want me to add a service file to the mythtv package


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#12 2012-05-10 20:52:12

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: mythtv with systemd problems [SOLVED]

graysky wrote:

@65kid - seems like Fedora should have their packages online like we do somewhere

They do.

http://pkgs.fedoraproject.org/gitweb/?p=mysql.git

Offline

#13 2012-05-10 23:07:40

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: mythtv with systemd problems [SOLVED]

@PirateJonno - yes please.
@FalconIndy - thanks!

So here is the fedora mysqld.service

[Unit]
Description=MySQL database server
After=syslog.target
After=network.target

[Service]
Type=forking
User=mysql
Group=mysql

ExecStartPre=/usr/libexec/mysqld-prepare-db-dir
# Note: we set --basedir to prevent probes that might trigger SELinux alarms,
# per bug #547485
ExecStart=/usr/bin/mysqld_safe --nowatch --basedir=/usr
ExecStartPost=/usr/libexec/mysqld-wait-ready $MAINPID

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

# We rely on systemd, not mysqld_safe, to restart mysqld if it dies
Restart=always

[Install]
WantedBy=multi-user.target

This is significantly different than the recommended arch version:

[Unit]
Description=MySQL Server
After=syslog.target network.target

[Service]
PIDFile=/var/run/mysqld/mysqld.pid
ExecStart=/usr/bin/mysqld --user=mysql --pid-file=/var/run/mysqld/mysqld.pid
ExecStop=/bin/kill -15 $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target

Can someone with some experience with systemd propose a hybrid?

Last edited by graysky (2012-05-10 23:16:30)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#14 2012-05-11 00:03:06

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: mythtv with systemd problems [SOLVED]

@graysky: I'm not sure about using the one you posted here.
Some people (e.g. me) don't use the network daemon. I think MythTV starts OK without it anyway. Or maybe I'm misinterpreting the meaning of network.target?
MYTHCONFDIR and --logpath should be set to directories...
HOME should probably be /var/lib/mythtv if anything (ideally I would be creating a mythtv user upon install, but I don't want to break other people's setups).
Could you change the file accordingly and see if it still works? Thanks


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#15 2012-05-11 00:09:19

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: mythtv with systemd problems [SOLVED]

@PJ -

*I don't know about that network.target - it might symlink to other daemons?
*Both MYTHCONFIDIR and HOME are dirs... am I missing something?

EDIT: I removed the trailing mythbackend.log from the first post so it just points to a dir.

$ ls -l /etc/conf.d/mythbackend 
-rw-r--r-- 1 root root 1920 Apr 15 21:12 /etc/conf.d/mythbackend


$ ls -l /usr/share/mythtv/
total 228
drwxr-xr-x  2 root root  4096 Apr 18 22:12 backend-config
drwxr-xr-x  3 root root  4096 Apr 18 22:11 bindings
-rw-r--r--  1 root root  7708 Apr 10 01:29 CDS_scpd.xml
-rw-r--r--  1 root root  4852 Apr 10 01:29 CMGR_scpd.xml
drwxr-xr-x  8 root root  4096 Apr 18 22:12 contrib
-rw-r--r--  1 root root  1741 Apr 10 01:29 devicemaster.xml
-rw-r--r--  1 root root   840 Apr 10 01:29 deviceslave.xml
drwxr-xr-x  2 root root  4096 Apr 18 22:12 fonts
drwxr-xr-x  3 root root  4096 Apr 18 22:12 hardwareprofile
drwxr-xr-x 10 root root  4096 Apr 18 22:12 html
drwxr-xr-x  2 root root  4096 Apr 18 22:12 i18n
drwxr-xr-x  4 root root  4096 Apr 18 22:12 internetcontent
drwxr-xr-x  2 root root  4096 Apr 18 22:12 locales
-rw-r--r--  1 root root   320 Apr 18 22:12 mc.sql
drwxr-xr-x  4 root root  4096 Apr 18 22:12 metadata
-rw-r--r--  1 root root  1039 Apr 10 01:29 MFEXML_scpd.xml
-rw-r--r--  1 root root  2599 Apr 10 01:29 MSRR_scpd.xml
-rw-r--r--  1 root root 14781 Apr 10 01:29 MXML_scpd.xml
-rwxr-xr-x  1 root root 58750 Apr 10 01:29 mythconverg_backup.pl
-rwxr-xr-x  1 root root 68457 Apr 10 01:29 mythconverg_restore.pl
-rw-r--r--  1 root root  1252 Apr 10 01:29 setup.xml
drwxr-xr-x 12 root root  4096 Apr 19 17:28 themes

Last edited by graysky (2012-05-11 01:04:44)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#16 2012-05-11 01:36:12

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: mythtv with systemd problems [SOLVED]

Success!  Not very sexy, but it works,  I updated the wiki with a method to get mysqld AND mythtvbackend to work together with systemd.

https://wiki.archlinux.org/index.php/Sy … ces#mysqld
https://wiki.archlinux.org/index.php/Sy … ythbackend

Last edited by graysky (2012-05-11 01:37:54)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#17 2012-05-11 09:54:38

wdirksen
Member
From: New Zealand
Registered: 2012-02-23
Posts: 105

Re: mythtv with systemd problems [SOLVED]

I personally don't use systemd, don't know anything about it, but have never had launching problems with MythTV using the daemon approach using the standard rc.d file for Arch on both server/backend en frontends. Mysqld is somewhere in the middle of the daemons list in rc.conf and because I have to launch other stuff on the server in a specific order, I put the following in rc.local and it works fine. (this is approximate quick from memory)

/etc/rc.d/oscam start
sleep 20
/etc/rc.d/sasc-ng start
sleep 5
/etc/rc.d/mythbackend start
sleep 5
/etc/rc.d/httpd start (for mythweb)

Then lxdm does it's thing

BTW #1: I find LDXE to be a good DE for a server as it doesn't seem to meddle with processes
BTW #2: There is a command to change lirc to use LIRC mode at the end of rc.local for the frontends or LIRC becomes flakey. I don't exact syntax out of my head.

Last edited by wdirksen (2012-05-11 10:05:03)


Research | Trial | Make Mistakes | Ask questions | Learn | Repeat

Offline

#18 2012-05-11 10:00:25

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: mythtv with systemd problems [SOLVED]

No offense dude, but that is not useful information for this thread.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#19 2012-05-11 10:33:26

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: mythtv with systemd problems [SOLVED]

graysky wrote:

Success!  Not very sexy, but it works,  I updated the wiki with a method to get mysqld AND mythtvbackend to work together with systemd.

https://wiki.archlinux.org/index.php/Sy … ces#mysqld
https://wiki.archlinux.org/index.php/Sy … ythbackend

I'm glad that it works for you. smile Anyway, since you already said yourself that it's "not very sexy", I'm gonna be a smartass and tell you why. big_smile

Type=simple

Not neccessary since it's the default.

PIDFile=/var/run/mysqld/mysqld.pid

This should only be neccessary for Type=forking , so it can be removed as well.

ExecStartPre=/usr/bin/mysqld-pre

Completely unneccessary as well, this is exactly what the tmpfiles.d/mysqld.conf file is for (that you removed from the wiki page). have a look at "man tmpfiles.d" .

ExecStart=/usr/bin/mysqld_safe --user=mysql

Are you sure that mysqld_safe is neccessary here? does mysqld not work? Because mysqld_safe seems to do stuff that systemd is supposed to do (restarting the service when it crashes etc, see "man mysqld_safe").
Also remove --user=mysql here and set User=mysql here. The fedora service file uses it as well so I assume this works.

Also let systemd handle the timeout, makes the -post script much shorter. This pid file stuff is is not neccessary in the script either.

#!/bin/sh
while true; do
	response=`/usr/bin/mysqladmin -uUNKNOWN_USER ping 2>&1` && break
	echo "$response" | grep -q "mysqld is alive" && break
	sleep 1
done
[Unit]
Description=MySQL Server

[Service]
User=mysql
ExecStart=/usr/bin/mysqld
ExecStartPost=/usr/bin/mysqld-post
Restart=always
Timeout=30

[Install]
WantedBy=multi-user.target

This works fine on my setup, please test it on your system too and change the wiki accordingly.

Offline

#20 2012-05-11 20:17:18

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: mythtv with systemd problems [SOLVED]

Great job, 65kid.  Thanks for the time!  Updated the wiki: https://wiki.archlinux.org/index.php/Sy … ces#mysqld

...and thanks for being a smartass smile

Last edited by graysky (2012-05-11 20:18:29)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

Board footer

Powered by FluxBB