Saturday, June 18, 2011

MyMedia Automatic Startup on Synology NAS

At this point, I assume you've followed my previous instructions and now have a working copy of MyMedia on your NAS, and you can connect to it and listen to audio from your Roku box.  Currently, though, you have to restart the server every time you power-cycle the NAS.  I have my NAS turn off nightly to save power (and be more quiet) so this obviously doesn't work well for me.

So, the next step is to add a script to the Synology's startup sequence to automatically bring the server up whenever the NAS starts.

1) First step is to create a startup script for our service.  The script should be able to start and stop the server.  Starting the server is easy -- we did that in our last article.  Stopping it, though, requires that we remember the process id (PID) of the server when we start it, so we can then kill that process.  Here's my script, which I called "rc.mymedia".  You can start a new file ("vi rc.mymedia") and copy/paste it into the editor.   I just keep the file in the home directory of the root user.

mymediadir=`dirname $mymedia`

case $1 in
        if [ -e $pidfile ] ; then
            echo "MyMedia server already running. PID=`cat $pidfile`"
        echo "Starting MyMedia server..."
        cd $mymediadir
        $python $mymedia >>$log 2>&1 &
        echo $pid > $pidfile
        cd $c

        if [ -e $pidfile ] ; then
            echo "Stopping MyMedia server..."
            kill `cat $pidfile`
            rm $pidfile
            echo "MyMedia server is not running.  No PID file."
        $0 stop
        sleep 1
        $0 start

2) We will make this an executable script
chmod a+x rc.mymedia

3) You can now start/stop/restart the server with this script
./rc.mymedia start
./rc.mymedia stop
./rc.mymedia restart

4) Next we'll add this script to our local startup sequence.  To do this, edit the file "/etc/rc.local" and add the following lines.  Be sure to use the correct path to your file from above.
# MyMedia Server
[ -x /root/rc.mymedia ] && /root/rc.mymedia start

5) And we're done!  Best to power-cycle your NAS now and see if the server does indeed startup automatically.  If there are issues, here are some things to check:
  • the server debug log at /opt/local/opt/mymedia/server/my_media_log.txt
  • the server error log at /var/log/mymedia.log
  • the PID file at /var/run/
  • the process list, with "ps | grep mymedia"


  1. This comment has been removed by a blog administrator.

  2. Note that the /root folder seems to get wiped during DSM upgrades, so bookmark this page... Sure am glad I did!

  3. Thank you Sir Bill for responding to my question the other day on the other post. I've managed to get the autostart script loaded into the root directory /root/rc.mymedia. And when I type "./rc.mymedia start", the server does indeed start and plays media files, but in Putty it reads "Starting MyMedia server..." , but then the next line reads "./ media: cd: line 37: can't cd to dirname" So, is the problem in Line 4 where I have typed "mymediadir=`dirname $mymedia` " verbatim, or is it in Line 16 where it says "cd $mymediadir", or indeed in Line 37 where it only says "esac".

    Secondly, I have changed the "/etc/rc.local" file and added the two lines in Step 4 exactly as it appears on the website (because my rc.mymedia file is indeed in the root folder). However, when I restart the NAS, it does not autostart the server. The only way I can get the server to work is if I fire up Putty on my PC and type "./rc.myserver start" and then keep Putty running. As soon as I close the Putty app, the MyMedia app on the Roku stops working.

    Any help would be greatly appreciated.

  4. @Terron: I imagine it's the "cd $mymediadir" which is failing. Being in the right folder is not required to start the server but merely ensures the logs end up there.
    Try "which dirname" from the command line to see if you have that command available.
    Also, be sure to use the backward tick character (usually over the "~" tilde character on US keyboards) and not the single quote character.
    If still stuck, try "bash -x rc.myserver start" to get a debug trace and post it back.
    Or just hard code the value: mymediadir="/opt/local/opt/mymedia/server"

  5. The backward tick character was the trick! I now do not get the error when I manually start the server in Putty AND when I rebooted the NAS it loaded up fine on the Roku and shows the appropriate folders. Thanks so much for helping me sort this out! Who woulda guessed there was a big difference between a ' and a `. I really appreciate all of your tutorials on this site! :D

  6. on Synology scripts placed in /usr/local/etc/rc.d are started and stopped automatically. So 'start' and 'stop' is issues automatically. The scripts need to have an .sh extension though. Don't put your script in directories where they arent supposed to be. Nice read about this: