New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init script #266

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

Comments

@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

This comment has been minimized.

Show comment
Hide comment
@jodal

jodal Dec 4, 2012

Member

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.

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

This comment has been minimized.

Show comment
Hide comment
@jodal

jodal Dec 4, 2012

Member

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.

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

This comment has been minimized.

Show comment
Hide comment
@albinlundmark

albinlundmark Mar 16, 2013

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?

albinlundmark commented Mar 16, 2013

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

This comment has been minimized.

Show comment
Hide comment
@adamcik

adamcik Mar 16, 2013

Member

#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.

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

This comment has been minimized.

Show comment
Hide comment
@woutervanwijk

woutervanwijk Mar 24, 2013

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
Contributor

woutervanwijk commented Mar 24, 2013

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
@dz0ny

This comment has been minimized.

Show comment
Hide comment
@dz0ny

dz0ny Apr 26, 2013

Member

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 )

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

This comment has been minimized.

Show comment
Hide comment
@jodal

jodal Apr 26, 2013

Member

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.

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

This comment has been minimized.

Show comment
Hide comment
@adamcik

adamcik Apr 26, 2013

Member

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:

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

This comment has been minimized.

Show comment
Hide comment
@dz0ny

dz0ny Jun 11, 2013

Member

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

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

This comment has been minimized.

Show comment
Hide comment
@terlar

terlar Sep 8, 2013

Contributor

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/).

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

This comment has been minimized.

Show comment
Hide comment
@txomon

txomon Sep 9, 2013

Member

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)

=)

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

This comment has been minimized.

Show comment
Hide comment
@coolbox

coolbox 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?

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

This comment has been minimized.

Show comment
Hide comment
@kingosticks

kingosticks Oct 11, 2013

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!

Member

kingosticks commented Oct 11, 2013

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

This comment has been minimized.

Show comment
Hide comment
@jodal
Member

jodal commented Oct 27, 2013

@jodal

This comment has been minimized.

Show comment
Hide comment
@jodal

jodal Dec 4, 2013

Member

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

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.

@adamcik

This comment has been minimized.

Show comment
Hide comment
@adamcik

adamcik Jan 9, 2014

Member

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?

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

This comment has been minimized.

Show comment
Hide comment
@jodal

jodal Jan 11, 2014

Member

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

Member

jodal commented Jan 11, 2014

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

@Franck69

This comment has been minimized.

Show comment
Hide comment
@Franck69

Franck69 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 ;-(

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

This comment has been minimized.

Show comment
Hide comment
@kingosticks

kingosticks May 5, 2014

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
.

Member

kingosticks commented May 5, 2014

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

This comment has been minimized.

Show comment
Hide comment
@fradeve

fradeve 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.

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