Start MyMedia as a daemon on Ubuntu Linux

271 views
Skip to first unread message

zmansour

unread,
Feb 22, 2011, 10:09:36 PM2/22/11
to MyMedia Roku Developers
Greetings all.

Can anyone suggest how to run MyMedia as a daemon on Ubuntu? I found
this script,
http://groups.google.com/group/mymedia-roku-developers/browse_thread/thread/3e92dbfd40f53395/34032b4157696c4a?hl=en&lnk=gst&q=linux#34032b4157696c4a,
but it seems a little outdated (as a run script, although I'm no Linux
guru) and starts rss_server.py rather than mymedia.py (specified in
step #3 of the installation guide). Also, I want this to run as a
specific user instead of root. My setup works just fine when I run in
an interactive shell, but fails if I try to start the server in the
background.

Any and all help is much appreciated.

Cheers,

Z.

ralyon

unread,
Feb 23, 2011, 2:21:23 PM2/23/11
to MyMedia Roku Developers
On Feb 22, 9:09 pm, zmansour <zmans...@yahoo.com> wrote:
> Greetings all.
>
> Can anyone suggest how to run MyMedia as a daemon on Ubuntu?  I found
> this script,http://groups.google.com/group/mymedia-roku-developers/browse_thread/...,
> but it seems a little outdated (as a run script, although I'm no Linux
> guru) and starts rss_server.py rather than mymedia.py (specified in
> step #3 of the installation guide).  Also, I want this to run as a
> specific user instead of root.  My setup works just fine when I run in
> an interactive shell, but fails if I try to start the server in the
> background.
>
> Any and all help is much appreciated.
>
> Cheers,
>
> Z.

Here are the changes I've made for my system. Note that it is not a
proper script and I only start it manually when I want it. I also
lazily used the mythtv instead of creating it's own user. Remember to
chown the mymedia folder and make sure that the user account you are
using has access to the videos, jic. ;)

Another option is to use the screen program to run mymedia.py and drop
out of that. Great program for running multiple terminals that you can
disconnect and reconnect to later.

ralyon

#!/bin/bash
#script to run roku server

ROKUDIR=/var/lib/mymedia/server
ROKUSCRIPT=mymedia.py
RUNAS_USER=mythtv

case "$1" in
start)
cd $ROKUDIR
start-stop-daemon --start --chuid $RUNAS_USER --exec $ROKUDIR/
$ROKUSCRIPT || return 2
;;
stop)
pkill -9 -f $ROKUSCRIPT || true
;;
restart|force-reload)
$0 stop
$0 start
;;
*)
echo "Usage: $NAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac

:

zmansour

unread,
Feb 25, 2011, 12:04:27 AM2/25/11
to MyMedia Roku Developers
OK, it took a bit but I think I got it. I followed your lead and
based my init script on what you have below as well as several other
scripts in my /etc/init.d folder. Here's what I did:

1. I wanted the server to run as a specific id, not one of my regular
IDs, so I edited /etc/passwd and added

mymedia:x:105:65534:MyMedia,,,:/nonexistent:/bin/false

Be sure your user id (105 in my case) is not already used and is less
than 1000. Also, be sure that 65534 maps to something like 'nogroup'
in /etc/group.

2. MyMedia wants to write to both config.ini and my_media_log.txt in
its 'sever' folder. So I created a 'mymedia' folder in /var/lib and
gave ownership of it to the mymedia id. Unzip the distro and chown as
well. The below should get you there, although I'm typing this from
memory:

mkdir /var/lib/mymedia
unzip -d /var/lib/mymedia <path to zip distribution>
<adjust to get rid of top level folder from unzip step; mv, rmdir,
etc>
chown -R mymedia:nogroup /var/lib/mymedia

3. The init script (start-stop-daemon, really) will try to write a
pid file as the mymedia id, which should go in /var/run. Create the
folder and adjust permissions:

mkdir /var/run/mymedia
chown mymedia:nogroup /var/run/mymedia

4. The init script will try to exec server/mymedia.py, but the unzip
from step 2 will leave all py files without the execute bit set.
Adjust with this

find /var/lib/mymedia -name '*.py' -print0 | xargs -0 chmod 755

5. Create the mymedia script in /etc/init.d. Take care that it's
owned by root and has the same permissions as the other scripts in
that folder. Here's the script:

#! /bin/sh

### BEGIN INIT INFO
# Provides: mymedia
# Required-Start: $all
# Required-Stop: $all
# Should-Start: $all
# Should-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Startup script for the MyMedia Server
# Description: MyMedia Server serves personal media to Roku
players.
### END INIT INFO

set -e

# /etc/init.d/mymedia: start and stop the MyMedia Server as a daemon

_DESC="MyMedia Server"
_NAME=mymedia
_SCRIPT=mymedia.py
_DIR=/var/lib/$_NAME/server
_PIDFILE=/var/run/$_NAME.pid
_SCRIPTNAME=/etc/init.d/$_NAME
_USER=$_NAME

. /lib/lsb/init-functions

#
# Function that starts the daemon/service.
#
d_start() {
start-stop-daemon --start --quiet \
--chuid $_USER \
--pidfile $_PIDFILE --make-pidfile \
--chdir $_DIR \
--background \
--exec $_DIR/$_SCRIPT
}

#
# Function that stops the daemon/service.
#
d_stop() {
start-stop-daemon --oknodo --stop --pidfile $_PIDFILE --retry=TERM/
30/KILL/5
}

case "$1" in
start)
log_daemon_msg "Starting $_DESC as a daemon" $_NAME
if [ -s $_PIDFILE ] && kill -0 $(cat $_PIDFILE) >/dev/null 2>&1;
then
log_progress_msg "apparently already running"
log_end_msg 0
exit 0
fi
d_start
log_end_msg 0
;;
stop)
log_daemon_msg "Stopping $_DESC daemon" $_NAME
d_stop
;;
restart|force-reload)
log_daemon_msg "Restarting $_DESC daemon" $_NAME
d_stop
d_start
log_end_msg 0
;;
*)
log_action_msg "Usage: $_SCRIPTNAME {start|stop|restart|force-
reload}"
exit 1
;;
esac

exit 0

6. Execute update-rc.d to create the links to the init script:

update-rc.d mymedia defaults

7. Bounce your box and check that MyMedia is running.

This works for Ubuntu 10.10 Server. Your mileage my vary with other
versions and distributions.

Cheers,

Z.
Reply all
Reply to author
Forward
0 new messages