Saturday, December 6, 2014

Synology NAS - How to make a program run at startup

The other day I created a little node.js project to keep track of some finances. Synology has a node.js package but that just installs the tools - it has no 'container' or any other support to drop files and have it run automagically. Maybe one day.

In the meantime, you can start your project when you SSH into the NAS. My project has a 'www' script which bootstraps my project, so to start I simply type 'node bin/www' from the project directory. But, it only runs while I'm logged in, and if I log out for any reason, the process dies. That's hardly useful when I'm away from home, or on a different PC. So I decided to have a look at starting my project as a Linux service.

After doing a lot of research into how Synology does services, and a few failed attempts at init scripts, I found that Synology DSM (since version 5 perhaps) bundles Upstart, which is a neat little tool to deal with services on Linux. It's most prevalent on Debian and derivatives (notably Ubuntu). So, here's how I got my node.js application running on startup by using Upstart.

Step 1. Create an Upstart script.


Upstart scripts live in /etc/init by default, and that's also the place they live on your Synology NAS. You name the script 'servicename.conf', where 'servicename' is whatever you want it to be called. I called mine 'foobar' because I'm inventive like that, so the file is /etc/init/foobar.conf.

You can be as simple or as comprehensive as you like. I started by using a very simple script, like the one below.

Step 2. Start the service manually


The best part about keeping it simple is that you are more likely to get it running. If there is an error in your script, it won't start and it won't tell you why. It will just say the service could not be found.

To start the script, just type start foobar from the terminal. If it's happy, you'll see the process start and the PID displayed on the console. To stop it again, type 'stop foobar'.

Step 3. Check the script will auto-start


If you pass step 2 OK, then this is just a formality. The script will start when you restart your box. When ready type 'shutdown -r now' and allow your NAS to reboot. When it comes up again, you will be able to see that your service is running by hitting the URL, or by checking the logs which, by default, go to /var/log/upstart - all sysout from your process will go here.

21 comments:

  1. That was exactly what I was searching for, thanks!

    I didn't tried a reboot of my NAS, but the start command didn't worked with setuid, but worked with default, although it's bad (as you mentioned).

    Furthermore I run my scripts with foreverjs/forever, so they restart on script failure, too.

    ReplyDelete
  2. Synology deleted all my custom scripts from /etc/init after system update. :(

    ReplyDelete
  3. Synology deleted all my custom scripts from /etc/init after system update. :(

    ReplyDelete
  4. I cant add my script "Gitblit.sh" to /etc/init ..
    The permission denied with login admin user
    Please help on this

    ReplyDelete
    Replies
    1. login admin no good but root user

      Delete
    2. Hello Bagulia, Have you found how to launch gitblit at statup on your Synology?

      Delete
  5. This is just fantastic!! Worked wonderfully well, although I ran into an issue. I had a different user and group assigned to my folder with my server, so the /var/log/upstart/.log file always said the module could not be found. Had to use chown root:root and chmod 755 to make sure those files were accessible by the http user, then I was able to get the script to run. I even went so far as to move the files into a directory I created in /usr. Now my path to them is /usr/github/ and that works great.

    Thanks for posting this!

    ReplyDelete
  6. I made a script. start manually works fine. But the service does not start with synology. I need to manually set it every time after boot.

    ReplyDelete
  7. To survive an update startup scripts should be located in /usr/local/etc/rc.d and need to be init script format (a shell script that handles the start/stop commands). Different from the above conf format. The Synology 3rd party developer guide has a section on start up scripts (even if it's about creating packages, it's a useful document) https://global.download.synology.com/download/Document/DeveloperGuide/DSM_Developer_Guide.pdf

    ReplyDelete
  8. Hi
    I am desprately needing help. I have node.js version 8 installed on D213J running DMS6+. I have a my code installed in /volume1/web/GoogleHomeKodi. The I am running server.js. It works via SSH for node /volume1/web/GoogleHomeKodi/sever.js

    I have crated auto start script in /etc/init named kodi.conf

    based on above info what should I have in kodi.conf file?

    I tried to create a .conf file looking at yours but cannot figure out from your script "exec /usr/bin/node /volume2/code/foobar/bin/www" what terms "usr", "volume2", "code" are as these are not part of the path. Also is www.js file?


    ReplyDelete
  9. Great site for these post and i am seeing the most of contents have useful for my Carrier.Thanks to such a useful information.Any information are commands like to share him.
    node js developer london

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. LE-MERIDIAN FUNDING SERVICES. We are directly into pure loan and project(s) financing in terms of investment. We provide financing solutions to private/companies seeking access to funds in the capital markets i.e. oil and gas, real estate, renewable energy, Pharmaceuticals, Health Care, transportation, construction, hotels and etc. We can finance up to the amount of $900,000,000.000 (Nine Hundred Million Dollars) in any region of the world as long as our 1.9% ROI can be guaranteed on the projects.
    Le-Meridian Funding Service -Email info@lemeridianfds.com.
    lfdsloans@outlook.com
    (WhatsApp...+1-989-3943-740 Or Call +1-913-9518-145)

    ReplyDelete
  12. Untuk menjadi seorang yang bisa mengambil keputusan bertaruh dengan baik tentu memerlukan pengetahuan dan pengalaman yang banyak
    asikqq
    dewaqq
    sumoqq
    interqq
    pionpoker
    bandar ceme terpercaya
    hobiqq
    paito warna terlengkap
    bocoran sgp

    ReplyDelete
  13. Are you looking for a business loan, personal loans, mortgage loans, car loans, student loans, unsecured consolidation loans,project funding etc ... Or simply refuse loan from a bank or financial institution for one or more reasons? We are the right solutions for credit! We offer loans to businesses and individuals with low and affordable interest rate of 2%. So if you are Interested in an urgent and secured loan. For more information kindly email us today Via: elegantloanfirm@hotmail.com.



    ReplyDelete