Configure Syncthing Auto-Start on Banana Pi Raspberry Pi System
Independent Observer, January 19, 2020 (Updated August 30, 2020)
First, let’s clarify some terms: “Banana Pi” is a domestic clone of the Raspberry Pi hardware product, “Raspberry Pi system” refers to “raspberrypi“, and “Syncthing” is a multi-platform sync software.
The installation and usage of Syncthing can refer to the online article “IT Home Academy: Build Your Own Private Cloud with Raspberry Pi Without Public IP“. This article will only discuss how to configure it for auto-start in the Raspberry Pi system (Linux system).
From “【Tutorial】Summary of Raspberry Pi Program Auto-Start Methods” we learn that there are generally four methods to set auto-start on the Raspberry Pi:
1. Add startup code to the /etc/rc.local file;
2. Create a script in the /etc/init.d path;
3. Configure to start with the desktop;
4. Use systemctl to set up services.
Previously, I used method 1 when configuring frp (a NAT traversal software), but it failed later, and now I am using method 4. Interestingly, I have never succeeded with method 4 for Syncthing. Do I need to use method 1 instead?
Let’s first open /etc/rc.local and see, and I found that Banana Pi seems to have done something else – using /var/lib/bananapi/bpi-autorun.d/bpi-autorun.sh as the startup script:
Now let’s open /var/lib/bananapi/bpi-autorun.d/bpi-autorun.sh:
It sequentially starts the scripts in the /var/lib/bananapi/bpi-autorun.d directory that start with “S” followed by a number:
So we create a startup script S90-syncthing.sh (don’t forget to give it executable permissions):
#!/bin/bash
sudo su - pi -c "exec /media/dlgcy/syncthing/syncthing"
My syncthing is located in the /media/dlgcy/syncthing directory, so I will also place S90-syncthing.sh there:
We can also see a start-syncthing.sh file:
sudo cp ./S90-syncthing.sh /var/lib/bananapi/bpi-autorun.d/S90-syncthing.sh
sudo /var/lib/bananapi/bpi-autorun.d/S90-syncthing.sh
This is used to copy the above startup script to the correct directory to ensure it can be called on the next boot, and it also starts it for this session.
Finally, let’s see what we are starting with such a hassle:
Updated August 30, 2020:
After some time of use, I found an issue with the above method: the software does not automatically restart after unexpected exit. Previously, I mentioned that I had never succeeded with the service method, but recently while working on “Downloading Proxy Accelerator VPSDownloader.NET (Deploying .NET Core Program to Linux System)”, I deepened my understanding of Linux system auto-start services, so I decided to switch to using the service method to start it.
This time, I added four files, namely the direct run script start-direct.sh, background run script start-background.sh, service file syncthing.service, and installation service script InstallService.sh, as follows:
First, the direct run script start-direct.sh:
#!/bin/bash
sudo su - pi -c "exec /media/dlgcy/syncthing/syncthing"
Then, the background run script start-background.sh:
#! /bin/sh
cd /media/dlgcy/syncthing
nohup ./start-direct.sh &
Next, the service file syncthing.service:
[Unit]
Description=Syncthing Service
After=network.target
[Service]
User=root
Type=forking
ExecStartPre=/bin/sleep 10
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
ExecStart=/media/dlgcy/syncthing/start-background.sh
Restart=always
RestartSec=30
StartLimitInterval=20
StandardOutput=/media/dlgcy/syncthing/log
[Install]
WantedBy=multi-user.target
Finally, the installation service script InstallService.sh:
chmod +x ./start-direct.sh
chmod +x ./start-background.sh
touch ./log
cp ./syncthing.service /usr/lib/systemd/system/syncthing.service
systemctl daemon-reload
systemctl enable syncthing
systemctl start syncthing
systemctl status syncthing
When using, we just need to cd to the corresponding directory and execute the following command (this only needs to be executed once):
sudo ./InstallService.sh
Execution successful:
Now, even if the program unexpectedly exits, it can automatically restart.
For an introduction to Syncthing, you can refer to the Zhihu article “(Twenty-Three) Niche but Useful: Syncthing Turns Your Phone into a Sync Cloud Disk”.
Wishing everyone a pleasant experience!