PS3 Media Server as a Linux Service

For help and support with issues specific to Linux/Unix
Forum rules
Please make sure you follow the Problem Reporting Guidelines before posting if you want a reply.

Re: PS3 Media Server as a Linux Service

Postby martin » Sat Feb 28, 2009 10:01 am

So here is my startup-script for SuSE. It worked for me. But on manual startup it say failure - but it runs. Maybe a Linux-Guru is here and he/she knows to fix this :roll:. I've used another script and do some changes. Btw. does anybody know what with the (c) in the header is, must it still there - then I'll put it in here...

It's importend to run this script as root. Go to the terminal and get root-rights - su. Copy the script to /etc/init.d/. I've named it pmsd. Then do chown root /etc/init.d/pmsd. Then do chmod +rwx /etc/init.d/pmsd.. Then go to yast2 and open Systemservices (in my german SuSE, Systemdienste (Runlevel). Go to Expertview, then click on pmsd and then click on activate Service. That should be all, on next starup the service should run in the background...

Code: Select all
#! /bin/sh
#
# init.d/pmsd
#
# System startup script for the PS3MediaServer Daemon
#
### BEGIN INIT INFO
# Provides: pmsd
# Required-Start: $remote_fs
# Should-Start: $syslog
# Should-Stop: $null
# Required-Stop: $null
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Start PS3MediaServer Daemon
# Description:    Start PS3MediaServer Daemon
### END INIT INFO

# Source SuSE config
. /etc/rc.status

PMSD_BIN=/usr/local/sbin/pms/PMS.sh
test -x $PMSD_BIN || { echo "$PMSD_BIN not installed";
    if [ "$1" = "stop" ]; then exit 0; else exit 5; fi; }

PMSD_CONFIG=/usr/local/sbin/pms/PMS.conf
test -r $PMSD_CONFIG || { echo "$PMSD_CONFIG not existing";
    if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; }

PMSD_PID=/var/run/pmsd/pmsd.pid

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
#

case "$1" in
    start)
   echo -n "Starting PS3MediaServer Daemon"
   /sbin/startproc -p $PMSD_PID $PMSD_BIN
   rc_status -v
   ;;
    stop)
   echo -n "Shutting down PS3MediaServer Daemon"
   /sbin/killproc -p $PMSD_PID -TERM $PMSD_BIN
   # if pmsd does not run as root, it cannot remove this files:
   rm -f /var/run/pmsd/socket $PMSD_PID
   rc_status -v
   ;;
    try-restart|condrestart)
   ## RH has a similar command named condrestart.
   if test "$1" = "condrestart"; then
            echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
   fi
        $0 status
        if test $? = 0; then
                $0 restart
        else
                rc_reset        # Not running is not a failure.
        fi
        rc_status
        ;;
    restart)
   ## Stop the service and regardless of whether it was
   ## running or not, start it again.
   $0 stop
   $0 start
   rc_status
   ;;
    force-reload)
   echo "Reload PS3MediaServer Daemon"
   $0 try-restart
   rc_status
   ;;
    reload)
   # pmsd does not support SIGHUP, so fail.
   echo -n "Reload PS3MediaServer Daemon"
   rc_failed 3
   rc_status -v
   ;;
    status)
   echo -n "Checking for PS3MediaServer Daemon: "
   /sbin/checkproc -p $PMSD_PID $PMSD_BIN
   rc_status -v
   ;;
    probe)
   test $PMSD_CONFIG -nt $PMSD_PID && echo restart
   ;;
    *)
   echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
   exit 1
   ;;
esac
rc_exit
martin
 
Posts: 7
Joined: Mon Feb 23, 2009 10:11 am

Re: PS3 Media Server as a Linux Service

Postby kes » Wed May 20, 2009 2:00 pm

Thanks for the script.

As the PS3 Media Server is a Java-app, you can force the Java process into running in the so-called "headless" state, which means it will not try to find the X-session. In other words, even if you have an X-session on the Linux server, it will run in console mode. Add the following Java option to the command-line in the PMS.sh-script: -Djava.awt.headless=true. The commandline would then become like (exec-line broken for readability):

Code: Select all
exec "$JAVA" $JAVA_OPTS -Xmx768M -Djava.encoding=UTF-8 -Djava.net.preferIPv4Stack=true \
   -Djava.awt.headless=true -jar "$PMS_JAR" "$@" &


I've also added to the script some variables to export the path of the script where I've put my PMS program. I also define a logfile that I pipe all output from the Java process to. Oh - and on my Ubuntu Hardy box the "log_daemon_msg" wasn't available in my default shell, so in my version of the script i source the relevant functions-file to get it.

My modified version of the script follows, in case it's usefull for somebody:
Code: Select all
#! /bin/sh

### BEGIN INIT INFO
# Provides:      pms
# Required-Start:   $network $local_fs $remote_fs
# Required-Stop:
# Default-Start:   2 3 4 5
# Default-Stop:      0 1 6
# Short-Description:   PS3 UPnP Media Server
### END INIT INFO

# Source init-functions:
source /lib/lsb/init-functions

# Script variables:
MONTH=`date +%Y-%m`
PMS_ROOT="/usr/local/pms"
PMS_LOG="/var/log/pms/pms-$MONTH.log"
DESC="PS3 UPnP Media Server"
NAME="PMS.sh"

# Path to executable:
DAEMON="$PMS_ROOT/$NAME"

# Run as user/group [UPDATE THESE TO MAKE VALID]:
PMSUSER=localuser
PMSGROUP=localgroup

# update PATH:
PATH="$PMS_ROOT:${PATH:+$PATH:}/usr/sbin:/sbin"

# export env-vars
export PATH PMS_ROOT PMS_LOG

# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then {
   echo "Couldn't find $DAEMON or not executable"
   exit 99
    }
fi

#
# Function that starts the daemon/service
#
do_start() {
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    cd "$PMS_ROOT"
    start-stop-daemon --start --quiet --chuid $PMSUSER:$PMSGROUP --exec $DAEMON --test > /dev/null \
   || return 1
    start-stop-daemon --start --quiet --chuid $PMSUSER:$PMSGROUP --exec $DAEMON \
   || return 2
}


#
# Function that stops the daemon/service
#
do_stop() {
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --signal 2 --retry 5 --quiet --name java
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    return "$RETVAL"
}

case "$1" in
    start)
   log_daemon_msg "Starting $DESC" "$NAME"
   do_start
   case "$?" in
       0|1) log_end_msg 0 ;;
       2) log_end_msg 1 ;;
   esac
   ;;
    stop)
   log_daemon_msg "Stopping $DESC" "$NAME"
   do_stop
   case "$?" in
       0|1) log_end_msg 0 ;;
       2) log_end_msg 1 ;;
   esac
   ;;
    *)
   echo "Usage: $SCRIPTNAME {start|stop}" >&2
   exit 3
   ;;
esac

exit 0


In order to make the logging work, the path you use in the PMS_LOG-variable needs to be created, and be writeable for the user running the Java-server, i.e. the user you put in the PMSUSER variable. I also make some tests in the PMS.sh-script (and also add the headless-option) in order to log to the logfile. The relevant changes from the default script follows (again, exec-line broken for readability):

Code: Select all
# Sanitycheck environment
[ "x$PMS_ROOT" = "x" ] && \
    echo "[ERROR] Environment variable 'PMS_ROOT' not set properly!" && exit 1
[ "x$PMS_LOG" = "x" ] && \
    echo "[ERROR] Environment variable 'PMS_LOG' not set properly!" && exit 1

# Execute the JVM
cd $PMS_ROOT
exec "$JAVA" $JAVA_OPTS -Xmx768M -Djava.encoding=UTF-8 -Djava.net.preferIPv4Stack=true \
  -Djava.awt.headless=true -jar "$PMS_JAR" "$@" 2>&1 >> "$PMS_LOG" &


~kes
kes
 
Posts: 1
Joined: Wed May 20, 2009 1:36 pm

Re: PS3 Media Server as a Linux Service

Postby Kryz » Wed Sep 23, 2009 7:41 pm

Thanks to all the people who have put their time and effort into getting this software to work, also on a headless linux machine. This was exactly what I was looking for. But obviously, I'm posting here because I have a bit of a problem. ;-) I'm running Ubuntu Jaunty with a 2.6.28-11-server kernel (headless), and I try to get as much of the software through the apt repository, since that makes it easy to manage. The problem: I can run PMS.sh without a problem, and it seems that most of the codecs work (haven't tried everything yet, but avi, mkv, mp3 and jpg I have tested successfully so far, and if I have other codec problems, I will dig into it myself first), but I can't run the software through the init.d script that has been provided in this thread. That's to say, it will run, but apparently it doesn't find my PMS.conf file, which it does find when running the software directly from the command line.

I've basically used most of kes' setup, only modifying the things that are different for my setup. I really can't find what's wrong.

My PS3MediaServerd:
Code: Select all
#! /bin/sh

### BEGIN INIT INFO
# Provides:      pms
# Required-Start:   $network $local_fs $remote_fs
# Required-Stop:
# Default-Start:   2 3 4 5
# Default-Stop:      0 1 6
# Short-Description:   PS3 UPnP Media Server
### END INIT INFO

# Source init-functions:
source /lib/lsb/init-functions

# Script variables:
MONTH=`date +%Y-%m`
PMS_ROOT="/usr/src/pms-linux-1.10.5"
PMS_LOG="/var/log/pms/pms-$MONTH.log"
DESC="PS3 UPnP Media Server"
NAME="PMS.sh"

# Path to executable:
DAEMON="$PMS_ROOT/$NAME"

# Run as user/group [UPDATE THESE TO MAKE VALID]:
PMSUSER=kryz
PMSGROUP=kryz

# update PATH:
PATH="$PMS_ROOT:${PATH:+$PATH:}/usr/sbin:/sbin"

# export env-vars
export PATH PMS_ROOT PMS_LOG

# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then {
   echo "Couldn't find $DAEMON or not executable"
   exit 99
    }
fi

#
# Function that starts the daemon/service
#
do_start() {
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    cd "$PMS_ROOT"
    start-stop-daemon --start --quiet --chuid $PMSUSER:$PMSGROUP --exec $DAEMON --test > /dev/null \
   || return 1
    start-stop-daemon --start --quiet --chuid $PMSUSER:$PMSGROUP --exec $DAEMON \
   || return 2
}


#
# Function that stops the daemon/service
#
do_stop() {
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --signal 2 --retry 5 --quiet --name java
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    return "$RETVAL"
}

case "$1" in
    start)
   log_daemon_msg "Starting $DESC" "$NAME"
   do_start
   case "$?" in
       0|1) log_end_msg 0 ;;
       2) log_end_msg 1 ;;
   esac
   ;;
    stop)
   log_daemon_msg "Stopping $DESC" "$NAME"
   do_stop
   case "$?" in
       0|1) log_end_msg 0 ;;
       2) log_end_msg 1 ;;
   esac
   ;;
    *)
   echo "Usage: $SCRIPTNAME {start|stop}" >&2
   exit 3
   ;;
esac

exit 0

My PMS.sh:
Code: Select all
#!/bin/sh

DIRNAME=`dirname $0`


# OS specific support (must be 'true' or 'false').
cygwin=false;
case "`uname`" in
CYGWIN*)
cygwin=true
;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
[ -n "$PMS_HOME" ] &&
PMS_HOME=`cygpath --unix "$PMS_HOME"`
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

# Setup PMS_HOME
if [ "x$PMS_HOME" = "x" ]; then
        PMS_HOME=`cd $DIRNAME/; pwd`
fi
export PMS_HOME

# Setup the JVM
if [ "x$JAVA" = "x" ]; then
        if [ "x$JAVA_HOME" != "x" ]; then
                JAVA="$JAVA_HOME/bin/java"
        else
                JAVA="java"
        fi
fi

# Setup the classpath
PMS_JAR="$PMS_HOME/pms.jar"

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
        PMS_HOME=`cygpath --path --windows "$PMS_HOME"`
        JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
        PMS_CLASSPATH=`cygpath --path --windows "$PMS_CLASSPATH"`
fi

# Sanitycheck environment
[ "x$PMS_ROOT" = "x" ] && echo "[ERROR] Environment variable 'PMS_ROOT' not set properly!" && exit 1
[ "x$PMS_LOG" = "x" ] && echo "[ERROR] Environment variable 'PMS_LOG' not set properly!" && exit 1

# Execute the JVM
exec "$JAVA" $JAVA_OPTS -Xmx768M -Djava.encoding=UTF-8 -Djava.net.preferIPv4Stack=true -Djava.awt.headless=true -jar "$PMS_JAR" "$@" 2>&1 >> "$PMS_LOG" &

A few notes:

- My JAVA_HOME environment variable is set and correct.
- PMS.sh is executable for the user in the init script (kryz).
- PMS.conf is in the /usr/src/pms-linux-1.10.5 directory.
- I don't get any specific errormessages when running the init script, other than a very clear hint that ; this is what the log file (in var/log/pms) says:
Code: Select all
[main] TRACE 18:37:23.068 Error in accessing debug.log...
[main] TRACE 18:37:23.070 Using temp folder for debug.log...
GUI environment no available
Switching to console mode
[main] TRACE 18:37:23.201 Starting Java PS3 Media Server v1.10.5
[main] TRACE 18:37:23.202 by shagrath / 2008-2009
[main] TRACE 18:37:23.202 http://ps3mediaserver.blogspot.com
[main] TRACE 18:37:23.202 http://code.google.com/p/ps3mediaserver
[main] TRACE 18:37:23.203
[main] TRACE 18:37:23.203 Java 1.6.0_16-Sun Microsystems Inc.
[main] TRACE 18:37:23.203 OS Linux amd64 2.6.28-11-server
[main] TRACE 18:37:23.204 Encoding: UTF-8
[main] TRACE 18:37:23.204 Temp folder: /tmp/javaps3media
[main] TRACE 18:37:23.304 Registering transcoding engine FFmpeg Audio
[main] TRACE 18:37:23.324 Registering transcoding engine MEncoder
[main] TRACE 18:37:23.325 Registering transcoding engine MPlayer Audio
[main] TRACE 18:37:23.326 Registering transcoding engine MEncoder Web
[main] TRACE 18:37:23.327 Registering transcoding engine MPlayer Video Dump
[main] TRACE 18:37:23.327 Registering transcoding engine MPlayer Web
[main] TRACE 18:37:23.332 Registering transcoding engine TsMuxer
[main] TRACE 18:37:23.333 Registering transcoding engine Audio High Fidelity
[main] TRACE 18:37:23.334 Registering transcoding engine VideoLan Audio Streaming
[main] TRACE 18:37:23.335 Registering transcoding engine VideoLan Video Streaming
[main] TRACE 18:37:23.335 Registering transcoding engine Raws Thumbnailer
[main] TRACE 18:37:23.562 Scanning network interface eth0 / eth0
[main] TRACE 18:37:23.565 Using address /192.168.0.2 found on network interface: name:eth0 (eth0) index: 2 addresses: /192.168.0.2;
[main] TRACE 18:37:23.566 Created socket: /192.168.0.2:5001
[Thread-1] TRACE 18:37:23.594 Starting DLNA Server on host 192.168.0.2 and port 5001...
[main] TRACE 18:37:23.602 Sending ALIVE...
[main] TRACE 18:37:23.649 Using following UUID: 77b62f5c-743d-318b-914a-691511738ca5
[main] TRACE 18:37:27.498 It's ready! You should see the server appears on XMB
[Thread-4] TRACE 18:37:37.494 Sending ALIVE...
[Thread-4] TRACE 18:38:02.416 Sending ALIVE...

It's clear here that the 'cd' command simply hasn't worked, so we're not in the PMS_ROOT directory. Another hint that this did not work is the following:
Code: Select all
kryz@orcrist:~$ /etc/init.d/PS3MediaServerd start
/etc/init.d/PS3MediaServerd: 13: source: not found
/etc/init.d/PS3MediaServerd: 94: log_daemon_msg: not found
/etc/init.d/PS3MediaServerd: 94: log_end_msg: not found

For some reason, the source command used in the script cannot be executed. And the other two errors, I don't know, my scripting / linux knowledge is far too basic.

Anyone able to help me out?

Small edit, as stipulated by the smart people of tweakers.net: source is a /bin/bash internal executable, and doesn't work under /bin/sh (unless /bin/sh is merely a symlink to /bin/bash), so I just changed the shebang to #!/bin/bash, and now I don't get the source / log_daemon_msg / log_end_msg errors anymore. But the core issue is not solved yet.

Edit 2: I've found the cause for the issue. For some reason, when in the init script, the current dir is indeed de PMS directory, but when in PMS.sh, pwd is /. I thought the 'if' statement in the PMS.sh would do something with that, but only now I get that it doesn't, so I just added a 'cd "$PMS_HOME"' line in the script, and everything seems to work. If anyone would like to see my new and improved versions of the scripts, don't hesitate to ask. I won't put them in this post now, since it's already full with the problem scripts. :)
Kryz
 
Posts: 2
Joined: Wed Sep 23, 2009 5:40 pm

Re: PS3 Media Server as a Linux Service

Postby russdot » Sun Sep 27, 2009 12:02 am

This thread is exactly what I was looking for! Thanks so much to all the people who contributed.

I'm going to note the change I made to kes's script (not necessary by any means, but might help)... I changed the signal that gets sent to the daemon. So instead of sending a SIGSTOP (signal #2) which merely 'pauses' the execution of the PS3 media server, I changed it to SIGTERM (signal #15) to actually shut down the PS3 media server (and your PS3 will reflect that by removing the icon for the media server and notifying you that the connection was lost)

So, the line:
Code: Select all
start-stop-daemon --stop --signal 2 --retry 5 --quiet --name java

Was changed to:
Code: Select all
start-stop-daemon --stop --signal 15 --retry 5 --quiet --name java


then using init.d: (I called mine 'ps3ms')
Code: Select all
sudo /etc/init.d/ps3ms start
starts the server and
Code: Select all
sudo /etc/init.d/ps3ms stop
stops the server.

Good luck!
russdot
 
Posts: 6
Joined: Sun May 24, 2009 4:55 am

Re: PS3 Media Server as a Linux Service

Postby edwardecl » Wed Sep 30, 2009 11:44 am

Service script works very nice but I have one... slight issue.

The stop script ends up closing all Java apps how do you make it so it only stops PMS?

I have Azureus running as a service and when you stop PMS is quits both apps.
edwardecl
 
Posts: 8
Joined: Wed Sep 30, 2009 11:31 am

Re: PS3 Media Server as a Linux Service

Postby WorldOfHurt » Sat Jan 02, 2010 10:50 pm

If you want to use start-stop-daemon with the "--pidfile $PIDFILE" option on Debian, also usethe "--make-pidfile" option because PMS.sh does not do it. And remember not to use the "-name $NAME" option, as the process does not run with the expected name - just use the pidfile.

Also DO NOT put the trailing ampersand on the last PMS.sh line.

Otherwise the pidfile will either not be created (no make-pidfile), or will contain the pid of the PMS.sh script not the java process (trailing ampersand).

Also, if you want to force headless without modifying the PMS.sh script, add this to your init.d script:
Code: Select all
JAVA_OPTS="-Djava.awt.headless=true"
export JAVA_OPTS


I started from the /etc/init.d/skeleton and wrote an init script that combines the best of the script above with the regular start-stop-daemon binary. If anyone wants it, I'll post it - just let me know.
User avatar
WorldOfHurt
 
Posts: 252
Joined: Thu Mar 19, 2009 10:49 pm
Location: Winchester, UK

Re: PS3 Media Server as a Linux Service

Postby Loops » Sun Jan 03, 2010 7:36 pm

Hy

I have write a start/stop script , it not use the pms.sh script.


Watch the viewtopic.php?f=3&t=4487 page. If you have an other jave applet, the stop not kill this one.

Regards/Cordialement.
Intel Core 2 Duo E6600 - 4Gb Ram - Debian Lenny NetInst with Multimedia source list - jre1.6.0_24
Loops
 
Posts: 12
Joined: Mon Oct 19, 2009 5:04 pm

Re: PS3 Media Server as a Linux Service

Postby lordemsworth » Wed Jan 06, 2010 7:29 am

Worldofhurt,

Is your script a Debian script?

Amir
lordemsworth
 
Posts: 28
Joined: Wed Jan 06, 2010 7:22 am

Re: PS3 Media Server as a Linux Service

Postby WorldOfHurt » Wed Jan 06, 2010 12:04 pm

lordemsworth wrote:Worldofhurt,

Is your script a Debian script?

Amir


Yes. Well, it's a linux shell script, so it works on my Debian etch server ;)

I'll post it later if its any use to you.
User avatar
WorldOfHurt
 
Posts: 252
Joined: Thu Mar 19, 2009 10:49 pm
Location: Winchester, UK

Re: PS3 Media Server as a Linux Service

Postby lordemsworth » Wed Jan 06, 2010 8:11 pm

if you would be so kind, it would be appreciated.

Amir
lordemsworth
 
Posts: 28
Joined: Wed Jan 06, 2010 7:22 am

PreviousNext

Return to Linux/Unix Support

Who is online

Users browsing this forum: No registered users and 4 guests