title: init.d: starting mplayer via "start-stop-daemon"
body: <p>with an <strong>Ubuntu</strong> machine I'd like to run <strong>MPlayer</strong> like a daemon.</p>
  
  <p>In foreground the following configuration is exactly what I need:</p>
  
  <p><code>mplayer -slave -idle -input file=/tmp/mplayercontrol</code>.</p>
  
  <p>Now, I wrote the following <strong>script</strong>:</p>
  
  <pre><code># /etc/init.d/mplayerd
  ### BEGIN INIT INFO
  # Provides:          mplayer
  # Required-Start:    $network $syslog
  # Required-Stop:     $network $syslog
  # Should-Start:      $time
  # Should-Stop:       $time
  # Default-Start:     2 3 4 5
  # Default-Stop:      0 1 6
  # Short-Description: Start and stop the mplayer daemon
  # Description:       mplayer daemon
  ### END INIT INFO
  
  CONTROLFILE="/tmp/mplayercontrol"
  DAEMONUSER=jb
  
  DAEMON=/usr/bin/mplayer
  DAEMON_ARGS="-slave -idle -input file=$CONTROLFILE"
  PIDFILE=/tmp/mplayerd.pid
  
  start() {
      echo "Starting mplayer..."
  
      # Prepare fifo file
      rm -rf $CONTROLFILE
      mkfifo $CONTROLFILE
      chmod a+rw $CONTROLFILE
  
      start-stop-daemon --start --quiet --user $DAEMONUSER    \
              --make-pidfile --pidfile $PIDFILE --background       \
              --exec /bin/bash -- -c "$DAEMON $DAEMON_ARGS &gt; /tmp/mplayerd.log 2&gt;&amp;1"
  
      echo "Started for user: $DAEMONUSER."
  }
  
  stop() {
      echo "Stopping mplayer..."
      kill -9 `cat $PIDFILE`
      # Cleanup fifo file
      rm -rf $CONTROLFILE
  }
  
  status() {
      if [ -z `cat $PIDFILE` ];
      then
          echo "mplayerd: not running."
      else
          echo "mplayerd: running."
      fi
  }
  
  
  case "$1" in
    start)
      start
      ;;
  
    stop)
      stop
      ;;
  
    restart|reload|force-reload)
      stop
      start
      ;;
  
    status)
      status
      ;;
  
    *)
      echo "Usage: /etc/init.d/mplayerd {start|stop|reload|force-reload|restart|status}"
      exit 1
  
  esac
  
  exit 0
  </code></pre>
  
  <p>But there is something wrong, because when I try to start the script the player return an error and doesn't start correctly:</p>
  
  <pre><code>root@jb:/tmp# service mplayerd start
  Starting mplayer...
  Started for user: jb.
  root@jb:/tmp#
  root@jb:/tmp# cat /tmp/mplayerd.log
  Cannot find HOME directory.
  MPlayer 1.0rc4-4.4.3 (C) 2000-2010 MPlayer Team
  root@jb:/tmp#
  </code></pre>
  
  <p>Additionaly, how can automatically run <code>/etc/inid.d/mplayerd stop</code> at the <strong>startup</strong> and <code>stop</code> at the <strong>shutdown</strong>?</p>
  
  <p>Thanks!</p>