Loading...
 

Greg`s Tech blog

Running PS3 Media Server as a non-root service on Fedora and Amahi

Tuesday 29 of March, 2011
<img src='tiki-view_blog_post_image.php?imgId=7' border='0' alt='image' /> <img src='tiki-view_blog_post_image.php?imgId=9' border='0' alt='image' />

Sorry for the detailed title, but this problem has been solved several times so I want to justify why I am solving it again.
I use PS3 Media Server (PMS) on Amahi to stream content to my Sony TV and PS3. I am helping to package the app for other Amahi apps.

In doing so, I was having difficulty running PMS as a non-root user on Amahi and discovered the following:

  • Fedora services use the runuser command to launch a new shell as the specified user and run the specified command in that shell (runuser commandline is in the /etc/init.d/functions script)

  • PMS locates the PMS.conf file within the current directory when it is executed. There is no current way to specify the location of the conf file.

  • When runuser executes a command, it drops the user into their home directory first and executes from there. For the apache user (which Amahi uses for these services), there is no home directory making it even more confusing.

With all this background, the solution was straight forward. We need to change the command we pass to runuser to cd to PMS_HOME then execute the java command. Here's the change I made to pmsd service script

daemon -20 --user $PMSUSER "cd $PMS_HOME && $JAVA $JAVA_OPTS"
The key being the addition of cd $PMS_HOME && brior to executing the java command


The entire service script is below:
Note: I did not write this script. Thanks to the Amahi & Fedora comunities for that)
#!/bin/bash
#
#       /etc/rc.d/init.d/pmsd
#
# Starts the PS3 Media Server
#
# chkconfig: 345 70 80
# description: PS3 Media Server
# processname: java

### BEGIN INIT INFO
# Provides: pmsd
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Default-Start:  3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop pmsd
# Description: PS3 Media Server
### END INIT INFO

#PMSUSER=pmsd
PMSUSER=apache
PMSGROUP=users
JAVA=`which java`

PMS_HOME="/var/hda/web-apps/ps3mediaserver/html"
PMS_JAR="$PMS_HOME/pms.jar"
PMS_JARS="$PMS_HOME/update.jar:$PMS_HOME/pms.jar:$PMS_HOME/plugins/*"
JAVA_OPTS="-Xmx768M -Xss16M -Djava.encoding=UTF-8 -Djava.net.preferIPv4Stack=true -classpath $PMS_JARS net.pms.PMS -Djava.awt.headless=true $@ >>/var/log/pmsd.log 2>>/var/log/pmsd.log &"	

PMSDPID=/var/run/pmsd.pid

export PMS_HOME

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0

start() {

        # Check if pms is already running
        if [ ! -f /var/lock/subsys/pmsd ]; then
            echo -n $"Starting PMS daemon: "
            daemon -20 --user $PMSUSER "cd $PMS_HOME && $JAVA $JAVA_OPTS"
            RETVAL=$?
            [ $RETVAL -eq 0 ] && touch /var/lock/subsys/pmsd
            echo
        fi
        return $RETVAL
}

stop() {

        echo -n $"Stopping PMS daemon: "
        killproc $JAVA
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/pmsd
        echo
    return $RETVAL
}


restart() {
        stop
        start
}

case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
restart)
        restart
        ;;
status)
        status $JAVA
        RETVAL=$?
        ;;
*)
        echo $"Usage: $0 {start|stop|status|restart}"
        RETVAL=2
esac

exit $RETVAL