-
-
Save SanCoder-Q/f3755435e6e8bd46ba95bf0ec54ae1a4 to your computer and use it in GitHub Desktop.
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. | |
``` | |
# only start this service after the httpd user process has started | |
start on started httpd-user | |
# stop the service gracefully if the runlevel changes to 'reboot' | |
stop on runlevel [06] | |
# run the scripts as the 'http' user. Running as root (the default) is a bad idea. | |
setuid http | |
# exec the process. Use fully formed path names so that there is no reliance on $PATH | |
# the 'www' file is a node.js script which starts the foobar application. | |
exec /usr/bin/node /volume2/code/foobar/bin/www | |
``` | |
view rawfoobar.conf hosted with ❤ by GitHub | |
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. |
Thanks. It worked well until DSM update.
I guess the update delete my conf file.
Hey, thanks so much.. It works pretty well. But i did not find the command for restart if failure or error. For maybe a mount. On Linux (pi) it looks like this and this is what i need for this synology .conf file:
ExecStop=/bin/fusermount -uz /xyz/abc
Restart=on-failure
Restart=on-abort
Awesome! With the help of this article I managed to use startup script for pm2. Here it is:
# must be in Synology NAS /etc/init/pm2-web-node-crawler.conf
# only start this service after the Node v12 process has started
start on started pkgctl-Node.js_v12
# stop the service gracefully if the runlevel changes to 'reboot'
stop on runlevel [06]
# run the scripts as the 'admin' user. Running as root (the default) is a bad idea.
setuid admin
# exec the process. Use fully formed path names so that there is no reliance on $PATH
exec env HOME=/var/services/homes/admin /usr/local/bin/pm2 start /var/services/homes/admin/web-node-crawler/observe.js
# vim:ft=upstart
Starting with DSM 7.0, upstart
has been replaced by systemd
and systemctl
.
Now, to achieve the same goal, you have to create a file in /etc/systemd/system
with the .service
extension like so
[Unit]
Description=Run foobar at startup
After=network.target
[Service]
Type=simple
User=http
ExecStart=foobar
Restart=on-failure
[Install]
WantedBy=multi-user.target
To start the service manually, use systemctl start foobar.service
To stop the service manually, use systemctl stop foobar.service
To check the status of the service use systemctl status foobar.service
When you are satisfied with your service file, enable the service for it to start automatically everytime the nas boots with systemctl enable foobar.service
Yes. Starting from DSM 7.0
you need to use systemd.
For NodeJS/PM2 work out of the box via (pm2 start ...
, pm2 save
) pm2 startup
!
Thank you for this copy of an article. It got me on a good way:) Discusion on original (majikshoe's; see bottom link) is also very interesting and gave me my answer;
On my DSM 6 I've solved it by placing .sh script (with perms 755) in /usr/local/etc/rc.d/
for details see section "Run Scripts When the System Boots" in DSM_Developer_Guide_6_0.pdf :
https://global.download.synology.com/download/Document/Software/DeveloperGuide/Firmware/DSM/6.0/enu/DSM_Developer_Guide_6_0.pdf
original source : https://majikshoe.blogspot.com/2014/12/starting-service-on-synology-dsm-5.html
you can schedule a task from the task scheduler and put script contents in there ( haven´t checked limits ).
It's under : Control Panel > Scheduled Tasks > Create > Scheduled Task > User-defined script
you may need root for some tasks. I use this ti send a wake command to some machines at start like so:
/usr/syno/sbin/synonet --wake [MAC address] eth0;
Works like a charm for me.
Thank you! Your tutorial really helped me out!