2013-03-24

How to make the mpdas run as a daemon

The other day I installed the mpdas, which is the audio scrobbler for the music player daemon. Since there's no debian package for the Raspberry Pi, I compiled mpdas from scratch and installed it. Now I don't want to run it manually each time the Raspberry Pi boots up. So I found a nice template for writing your own debian-style init-script. I changed it a little and also installed the daemon tool, to turn the interactive mpdas program into a daemon. Just run apt-get install daemon to install it. Then put the following file under /etc/init.d/mpdas and run update-rc.d mpdas defaults. Then mpdas will be run automatically upon boot. Oh, one more thing: put your mpdas configuration under /usr/local/etc/mpdasrc or adjust the DAEMONOPTS in the init script accordingly.
#!/bin/bash
### BEGIN INIT INFO
# Provides:          mpdas
# Required-Start:    $remote_fs $syslog $mpd
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Audio Scrobbler for mpd
# Description:       Starts the Audio Scrobbler for the mpd music player daemon.
### END INIT INFO

DAEMON_PATH="/usr/bin/"

DAEMON=daemon
DAEMONOPTS="-u pi -r -X /usr/local/bin/mpdas"

NAME=mpdas
DESC="The mpdas audio scrobbler for mpd"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
 if [ -z $PID ]; then
     printf "%s\n" "Fail"
 else
     echo $PID > $PIDFILE
     printf "%s\n" "Ok"
 fi
;;
status)
 printf "%-50s" "Checking $NAME..."
 if [ -f $PIDFILE ]; then
     PID=`cat $PIDFILE`
     if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
  printf "%s\n" "Process dead but pidfile exists"
     else
  echo "Running"
     fi
 else
     printf "%s\n" "Service not running"
 fi
;;
stop)
 printf "%-50s" "Stopping $NAME"
     PID=`cat $PIDFILE`
     cd $DAEMON_PATH
 if [ -f $PIDFILE ]; then
     kill -HUP $PID
     printf "%s\n" "Ok"
     rm -f $PIDFILE
 else
     printf "%s\n" "pidfile not found"
 fi
;;

restart)
$0 stop
$0 start
;;

*)
 echo "Usage: $0 {status|start|stop|restart}"
 exit 1
esac

2 comments:

  1. Hi Arne,

    I followed your guidelines but mpdas is not scrobbling anything as a daemon. But it worls fine when doing just :
    $ mpdas &

    Have you encountered that kind os issue yet?
    Best,
    Denis

    ReplyDelete
  2. Hi Denis,

    no, I haven't. Does mpdas run after starting the daemon using the script?

    It might be necessary to debug the script. See this Stackoverflow answers for hints: http://stackoverflow.com/questions/951336/how-to-debug-a-bash-script

    ReplyDelete