bone.recipes



Run Syncthing in Termux on Android

In the wake of some security concerns about the Syncthing-Fork app on Android, I decided to try running Syncthing in Termux. It works just fine! It's obviously a little less elegant than having an Android app, but it's significiantly less painful than expected. Here's what I did:

Install Termux & Syncthing

1. Install the Termux app.

2. Open the Termux app and install Syncthing.

pkg update
pkg install -y syncthing

3. (Optional) At this point, if you don't want to continue on to the next part in which we create a home screen shortcut / launcher, then you can just start Syncthing right away in Termux:

syncthing

Create a home screen shortcut

4. Install the Termux:Widget app.

5. Open the Termux app again. Create a new script in the ~/.termux/widget/dynamic_shortcuts directory (after creating the directory if it doesn't exist):

# create the directory if it doesn't exist:
mkdir -p ~/.termux/widget/dynamic_shortcuts

# move into the directory:
cd ~/.termux/widget/dynamic_shortcuts

# create and edit the new script with any editor (e.g., `nano`):
nano syncthing.sh

6. Set the contents of the script to be something like this (modified to your taste, if you like):

# set the syncthing gui address:
SYNCTHING_GUI_ADDRESS="http://localhost:8384"

# if the syncthing process isn't running, then start it:
if ! [[ `pidof syncthing` ]]; then
  syncthing --gui-address=$SYNCTHING_GUI_ADDRESS &
fi

# open the syncthing gui address in the default web browser:
xdg-open $SYNCTHING_GUI_ADDRESS &
wait

Save the changes and close the editor.

7. Add execute permissions to the script:

chmod +x syncthing.sh

8. Open the Termux:Widget app. Scroll down and click on the "CREATE SHORTCUTS" button.

9. Find the Termux:Widget app on your home screen or in a list of apps. Long-press on it. A list of scripts will appear. Your new script, syncthing.sh (or whatever you called it), should be in the list. Drag it from the list and drop it on the home screen.

10. Tap the new home screen shortcut to start Syncthing!

Notes & caveats

Persistent notification: Starting Syncthing this way starts a Termux session that remains open at least as long as Syncthing itself remains running. You can "swipe away" the Termux window, but a persistent notification will stick around as long as the Termux session remains open. Personally, this doesn't bother me because I usually ran the Syncthing-Fork app in always-on mode, which also resulted in a persistent notification. But if this bothers you, then maybe you could look into running Syncthing as a service.

GUI address: You don't have to use my solution of hard-coding the GUI address into the script. But because I wanted the shortcut always to open the GUI address in the browser (regardless of whether or not the Syncthing process had already been started), I needed to know the value of that address; and the only way of finding it at runtime would've been to parse Syncthing's XML config file, which would've required a much more complex script. So I opted for the simple solution of hard-coding the address. But if you know of any syncthing commands that just print out the GUI address, please let me know!