Add init script #266

Closed
jodal opened this Issue Dec 4, 2012 · 20 comments

Projects

None yet
@jodal
Member
jodal commented Dec 4, 2012

The init script should:

  • run Mopidy in the background as a daemon
  • run Mopidy as its own user
@jodal
Member
jodal commented Dec 4, 2012

Comment by @ruudud 2012-01-30 in the old mopidy-deb repo:

According to a superuser.com question, the way to run an Upstart job as an unprivileged user is as follows:

exec su -s /bin/sh -c 'exec "$0" "$@"' username -- /path/to/command [parameters...]

Also, it is nice to make sure that the network is up before Mopidy is started, so we end up with something like this:

$ cat /etc/init/mopidy.conf

description "Mopidy"                                                               

start on (local-filesystems and net-device-up IFACE=eth0)                          
stop on runlevel [!2345]                                                           

exec su -s /bin/sh -c 'exec "$0" "$@"' mopidy -- mopidy                            

I created the user mopidy as a system user -- sudo adduser --system mopidy -- and also had to create some directories to make Mopidy start at all:

$ tree /home/mopidy/
├── .config
│   └── mopidy
│       └── settings.py
└── .local
    └── share
        └── mopidy

Lastly, I had to add the mopidy user to the audio group -- sudo adduser mopidy audio -- but this may be specific to my Ubuntu Server setup.

Mopidy can now manually be started with sudo start mopidy, and will automagically start at next startup.

@jodal
Member
jodal commented Dec 4, 2012

Comment by myself 2012-01-30 in the old mopidy-deb repo:

Issues #267, #268, and #269 should together make it possible do without the /home/mopidy dir.

@albinlundmark

The only thing I'm missing in your task description above is handling logging when running it as a daemon. Is this handled through the config file or should one pipe standard output/error somewhere?

@adamcik
Member
adamcik commented Mar 16, 2013

#245 could/should probably take this into account, or cover it with #280, or we need a new bug or for that case. Currently #245 mainly covers the idea of having easier access to changing log levels via configs and/or flags. For general configuration of log files we could be using logging.config, but we would need to decide if we want to build a dict from our own config or have a dedicated logging config file. So yeah, I haven't quite landed on what I want for this, but yes this needs to be addressed, and most likely I would say we output to file.

@woutervanwijk
Contributor

seen this? http://delarre.net/posts/setting-up-a-raspberry-pi-media-center.html


#!/bin/bash
# mopidy daemon
# chkconfig: 345 20 80
# description: mopidy daemon
# processname: mopidy
### BEGIN INIT INFO
# Provides:          mopidy deamon
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start mopidy daemon at boot time
# Description:       Enable mopidy music server
### END INIT INFO

DAEMON_PATH="/usr/bin/"

DAEMON=mopidy
DAEMONOPTS=""

NAME=mopidy
DESC="My mopidy init script"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
        echo "Starting Mopidy Daemon"
        start-stop-daemon --start --chuid mopidy --background --exec /usr/bin/mopidy \
                --pidfile $PIDFILE --make-pidfile \
                -- 2>/var/log/mopidy.log
;;
stop)
     echo "Stopping Mopidy Daemon"
        start-stop-daemon --stop --exec /usr/bin/mopidy --pidfile $PIDFILE
;;

restart)
        $0 stop
        $0 start
;;

*)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac
@adamcik adamcik referenced this issue in pimusicbox/pimusicbox Apr 24, 2013
Closed

Delay Spotify login till after network is up #15

@dz0ny
Member
dz0ny commented Apr 26, 2013

Upstart script

description  "Mopidy start script"
author         "dz0ny"

start on (local-filesystems and net-device-up)
stop on runlevel [!2345]

respawn

script
  test -x /usr/local/bin/mopidy || exit 0
  start-stop-daemon --start --chuid mopidy --background --exec /usr/local/bin/mopidy
end script

Are there any pre-checks needed? (creating user etc)
Handle crash?
Should script use $(which mopidy) to determine mopidy bin path?
Should script check for /etc/defaults/mopidy (should it start, path to config, etc )

@jodal
Member
jodal commented Apr 26, 2013

The Debian package should handle creating the user.

For the rest of your questions, I'll have to look at what existing upstart scripts do.

@adamcik
Member
adamcik commented Apr 26, 2013

Most likely we want to use /etc/init.d/skeleton as a template, and not use upstart, just from the point of view of having a single init script for debian and ubuntu. Otherwise see:

@dz0ny
Member
dz0ny commented Jun 11, 2013

systemd start script (arch and others)

Create
$ nano /etc/systemd/system/mopidy.service

[Unit]
Description=Mopidy        
After=network.target

[Service]
ExecStart=/usr/bin/mopidy --config=/etc/mopidy.conf

[Install]
WantedBy=multi-user.target

Enable
$ systemctl enable /etc/systemd/system/mopidy.service

@terlar
Contributor
terlar commented Sep 8, 2013

I think that the network available part should not be up to the init-script.

  1. You want to be able to start it for usage with local files only.
  2. You might start mopidy in an environment without network access at the moment, but get to have access later.

Right now if it didn't succeed connecting to Spotify/Last.fm etc at boot, it will be dead until you manually restart mopidy.

As for network.target in systemd it seems it is not completely reliable (http://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/).

@txomon
Member
txomon commented Sep 9, 2013

Please, take into account that network is initialized as soon as localhost sockets are available. This doesn't mean to have a connection. In case of not having it, it will just fail to connect to the remote host in case no rute availiable.

Moreover, mopidy with his http frontend, mpd frontend, etc. needs networking initialized to be able to bind to the configured addresses. So in my opinion, After=network.target is totally needed (or equivs)

=)

@coolbox
coolbox commented Oct 11, 2013

I've tried following the upstart suggestion in comment posted by @jodal at the top of this issue but Mopidy doesn't want to start on my Pi. FYI running mopidy on the CL runs fine. I'm new to upstart and i'm not sure how to debug the problem, can someone point me in the right direction please?

This is as far as i get running the manual command:

sudo start mopidy
mopidy start/running, process 1407
initctl list | grep mopidy
mopidy stop/waiting

Will errors be output in a log somewhere that i can look at?

@kingosticks
Member

I use a start-stop daemon script on my RPi. It's here https://github.com/kingosticks/mopidy/blob/develop/init.d.mopidy if you're interested @coolbox. Note I actually have RUN_AS=xbian as I can't be bothered with multiple users!

@jodal
Member
jodal commented Dec 4, 2013

With the merge of #590, the Debian package got an init script. The remainder here should probably be moved into the docs.

@jodal jodal was assigned Dec 4, 2013
@adamcik
Member
adamcik commented Jan 9, 2014

Is this missing anything other than mopidy --config $(printf "%s:" /etc/mopidy/extensions.d/*.conf /etc/mopidy/mopidy.conf) to allow for /etc/mopidy/extensions.d/ config files?

@jodal
Member
jodal commented Jan 11, 2014

At least according to my memory that's the remaining thing, yes.

@jodal jodal added a commit that referenced this issue Jan 11, 2014
@jodal jodal debian: Use config from /etc/mopidy/extensions.d
Fixes #266
9cbe20a
@jodal jodal closed this Jan 11, 2014
@Franck69
Franck69 commented May 5, 2014

hi guy,
thx for your job, it's impressive!
sorry i'm noob in unix,
i've instal mopidy on raspbmc, it work fine!
but i don't know how mopidy launch on boot,
what i need to do for this ??

thx a lot

Edit:
i have this at the boot: warnig: 'alsact1 restor' failed with error message 'alsact1: load_state:1686:no soundcard found

it restart, and after is good, but no mopidy ;-(

@kingosticks
Member

Have you read the documentation at
http://docs.mopidy.com/en/develop/debian/#running-as-a-system-service-by-default?
You should find everything you need to know there.
On 5 May 2014 19:44, "Franck69" notifications@github.com wrote:

hi guy,
thx for your job, it's impressive!
sorry i'm noob in unix,
i've instal mopidy on raspbmc, it work fine!
but i don't know how mopidy launch on boot,
what i need to do for this ??

thx a lot


Reply to this email directly or view it on GitHubhttps://github.com/mopidy/mopidy/issues/266#issuecomment-42222514
.

@fradeve
fradeve commented Jan 11, 2015

I'm using this: https://gist.github.com/Freso/7434553
and unfortunately on my Arch it is not working, don't know why. It would be useful to have a default "--user" Systemd script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment