Skip to content

Instantly share code, notes, and snippets.

@Assassin9520
Forked from SanCoder-Q/synology.startup
Last active March 14, 2024 12:55
Show Gist options
  • Save Assassin9520/e5115db3f6d86277c4f49c705c27a968 to your computer and use it in GitHub Desktop.
Save Assassin9520/e5115db3f6d86277c4f49c705c27a968 to your computer and use it in GitHub Desktop.
Synology NAS - How to make a program run at startup
Synology NAS - How to make a program run at startup
Forked from https://gist.github.com/SanCoder-Q/f3755435e6e8bd46ba95bf0ec54ae1a4
To make a startup service on DSM7.2 see below comment.
(TL;DR):
touch /etc/systemd/system/startup_service_mine.service
systemctl enable startup_service_mine.service
systemctl status startup_service_mine.service
@Assassin9520
Copy link
Author

Assassin9520 commented Mar 14, 2024

Issue:

When upgrading from DSM 6.2 -> DSM 7.2 on Synology NAS (DS2++), UPS USB not recognized anymore.

Handle/Solution:

  Add a service at startup of NAS OS to restart the service after system normal boot sequence is finalized properly by Synology.

Info: in this solution, we will use systemctl to handle the service start at boot. on DSM7.2, in order to restart a host service, we need to use synosystemctl restart [user-service]

Let's go:

1. Open ssh connection to Synology DS220j
Go to the control panel of your NAS and open telnet and ssh. Login into your NAS(enable also root and make sure to disable the rights after you finish the work for this operation)

2. Adding/Writing the service file
Create a file in /etc/systemd/system using the .service extension (e.g.: restart-UPSUSB_service_at_boot.service)
Body contents should be:

[Unit]
Description=Restart ups service at boot
After=network.target

[Service]
Type=simple
User=root
ExecStart=/bin/sh /var/scripts_local/restart-UPSUSB.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Also, you will want to keep the ownership like the original system services:
chmod 751 restart-UPSUSB_service_at_boot.service
.
After doing so, in order to stick this at start of NAS system:
systemctl enable restart-UPSUSB_service_at_boot.service
You will see that the symlink has been created.
.
Check the status anytime with:
systemctl status restart-UPSUSB_service_at_boot.service

3. Writing the systemctl sh service
You can see that our service is calling a shell script. -> restart-UPSUSB.sh
The body contents of .sh should be:

#!/bin/sh

#waiting for normal boot process of NAS to finish, safe time
sleep 180

#call synosystemctl to restart the ups service
/usr/syno/bin/synosystemctl restart ups-usb

#success message stdout print
echo "ups-usb driver reset with success."

.
Let us loop this step by step:
-First and foremost, we want to hand the service from restarting the ups service until the BOOT process of NAS is succefully done and uninterrupted, by using sleep 180. (we wait 3 minutes to restart usb ups, interrupting NAS boot behaviour might not trigger our restart)
-Second, we need to call synosystemctl with full abs path, otherwise systemctl is not recognizing the binary: /usr/syno/bin/synosystemctl restart ups-usb
-also, make sure to change ownership of the sh script to admin (use chown)
.

4. Final Steps. Close ssh connection(keep yourself secure)
Now, that all is set, enable the service like in step2, close your ssh for your safety and restart the NAS.
After the normal boot, your should see in Control panel that NAS is connected succesfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment