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.