Keeping Syncthing Running: Ubuntu / Upstart

I’d like to run syncthing automatically, and keep it running.

I’m sure most people reading this already have their own ways of doing this, but I thought I’d explain how I do it for reference. I’ve got two machines: call them “desktop” (my main computer which I sit at all the time; running Ubuntu 13.10) and “server” (my home server, in a cupboard, running Ubuntu 12.04), which share repositories. Syncthing does already do a job of restarting itself if required, but in a weird way: if you run syncthing, and it needs to restart, it’ll spawn a new process, but the old one doesn’t end. So if you kill the syncthing process you started, syncthing is still running and you can’t restart it because the other process is bound to the port; lots of restarts mean lots of running syncthing processes.

Anyway, the key to this is not to have syncthing restart itself, but to have your preferred process supervisor do it. If you have a preference for process supervisors, then use your preference and ignore this, of course.

Keeping Syncthing running on an Ubuntu 13.10 desktop

Add this text as .config/upstart/syncthing.conf:

start on desktop-start
stop on desktop-end
env STNORESTART=yes
respawn
exec /path/to/executable/syncthing

This uses Upstart to run syncthing when you log in, and to restart it if it needs to be restarted (respawn). The key thing there is STNORESTART, which when set as an environment variable will make syncthing just quietly quit when it needs to restart rather than trying to handle restarting itself; then upstart will restart it.

Keeping Syncthing running on an Ubuntu 12.04 server - horrible poor-man’s edition

On the server I want to still run syncthing as me (not as root), and I don’t like using root stuff to run user services, and I don’t use a process supervisor such as monit or runit or anything. So I just put this line in my user’s crontab:

* * * * * wget -q -O- http://localhost:8180 > /dev/null || STNORESTART=yes /path/to/syncthing

Once a minute this hits the GUI port (which I have set to 8180) and if the GUI’s not available, starts syncthing (with STNORESTART as before). This is a horrible low-budget way of doing process supervision, but it works for me.

7 Likes

I moved a post to a new topic: Keeping Syncthing Running: Systemd & regular /etc/init.d

I moved 2 posts to a new topic: Keeping Syncthing Running: Upstart

I moved a post to an existing topic: Keeping Syncthing Running: Upstart

I moved a post to a new topic: Keeping Syncthing Running: /etc/rc.local

I moved a post to a new topic: Keeping Syncthing Running: Windows 7 & Cygwin

I’ve been running synching on my headless RaspberryPi using the “horrible poor-man’s” solution. (Thank You @sil) I’ve decided rewrite the crontab to suit my use case better and this is what I came up with.

This first crontab opens syncthing in a screen, that way you can read synching’s output and see whats happening (very useful when the gui is not available) The second one checks every 5 min to see if synching’s gui is accessible, if not it assumes that syncthing crashed and it will be restarted

* * * * * screen -ls | grep sync || /usr/bin/screen -dmS sync /path/to/syncthing

5 * * * * wget --spider localhost:8080 || if killall syncthing; then /usr/bin/screen -dmS sync /path/to/syncthing; fi 

The main reason I wrote the crontabs this way is because the last time I did a major update (v0.8 to v0.9) syncthing took forever to regenerate its certificate, unfortunately the “horrible poor-man’s" didn’t take this into accountm, so it would kill it before it finished.

So, here is an alternate way to achieve the a simular result (probably in a less efficient manner). If you see any errors or possible improvements please feel free to give input.

As a note here, syncthing is nowadays pretty good at keeping itself running. In fact, I would be very surprised if it ever just died by itself - it has a monitor process and does restarts on crashes etc as appropriate. So, an @reboot line in crontab should be just fine without the need to periodically poll for it still being alive.

@calmh yeah. The horrid solution was written a long time ago; syncthing doesn’t need it any more, I don’t believe! Note, though, the issue mentioned in the first post; when syncthing restarts itself, the old process doesn’t die. Is that changed now?

The STNORESTART behavior remains as you write above. If that variable is set, syncthing will not use a monitor process and will simply exit when restart is requested (and crash if it crashes, etc). If STNORESTART is unset, a monitor process is used. So from the outside, syncthing does not exit or restart. Instead, a child process is started and managed.

A downside to an STNORESTART setup is that syncthing doesn’t know it’s restarting when it’s restarting; it just starts up as if it was the first time ever. This means it opens the browser if configured to, etc.

Hi - I have just used the above posts to create my own Upstart config file, for a new install of Ubuntu 14.10 - so the information still works fine! Thanks for providing it here.

Below is a copy that includes the notes I made for myself (based on the above posts) - just in case any one else find it helpful too. Just cut and paste the text below into a file named syncthing.conf and follow the instructions!

[updated post to reduce line wrapping in text below]

#
#       Upstart Configuration File for Ubuntu 14.10 - to run syncthing
#
#  Created 27 Dec 2014 - initial file written based on version from 
#  Syncthing forums here: 
#  https://forum.syncthing.net/t/keeping-syncthing-running-ubuntu-upstart/30
#
#
# BEFORE USE: change:
#
#   1) the 'env SYNCTHING_EXE=' line below so it has the correct path 
#      to wherever you have installed the syncthing executable on your
#      computer - maybe '$HOME/bin'?
#      Enter the full path, as the use of $HOME wont work (I believe?) 
#      as Upstart does not inherit your normal environment variables.
#
#   2) the 'env SYNCTHING_CONF=' so the path shown to the 'syncthing' 
#      configuration files is right.  The default location is: 
#             $HOME/.config/syncthing
#      In the line below, the text "/home/simon/.config/syncthing" needs
#      to match your own home directory location. So on Ubuntu - the 
#      directory path needs to be changed, replacing the word  'simon'
#      with your own username. The use of $HOME wont work (I believe?)
#      as Upstart does not inherit your normal environment variables.
#
# INSTALLATION:
#  To install and use - copy (or symlink) this file (once updated) to
#  this path on you computer: 
#
#         $HOME/.config/upstart/syncthing.conf
#
#  If you want to find that folder in the graphical Ubuntu 'Files'
#  application (ie Nautilus), you will need to first press the keys
#  'Ctrl+h' on you keyboard to see hidden items. 
#  The '.config' folder should then be visible, and enable you to 
#  navigate into it. Press the keys 'Ctrl+h' on you keyboard to re-hide
#  the hidden items when done. Other wise just copy the file in a Terminal
#  window with the 'cp' command
#
# NB: You will need to login out of your desktop and back in to start 
#     syncthing running!
#
# LOGGING INFO:
#  Any output will be logged to: $HOME/.cache/upstart/syncthing.log 
#  (or syncthing.log.1.gz) To view the last 50 log entries in the file 
#  from a terminal window use the command:
#
#       tail -50 $HOME/.cache/upstart/syncthing.log
#
#  or if the system has gziped the log file, you can use the equivalent
#  command:
#
#        zcat $HOME/.cache/upstart/syncthing.log.1.gz | tail -50
#
# NOTES:
#  syncthing startup options used:
#
#   -no-browser : stops a browser window being opened if syncthing starts
#   up or restarts
#   -home="$SYNCTHING_CONF" : default syncthing configuration files location
#
# You can also add below the following syncthing environment variable options
# if required:
#
#   env STNORESTART=yes 
#          : if syncthing crashes or tries to restart, it will just exit 
#          instead
#   env STTRACE=beacon,net
#          : will enable debug output for 'beacon' & 'net'. See the Syncthing
#          forum post here for more info: 
#          https://forum.syncthing.net/t/debugging-syncthing/222
#
# You can run syncthing from a terminal window to see a full list of the 
# command line options that are available. The command to use is:
#
#    syncthing --help
#
#
# CHANGE BELOW TO MATCH YOUR REQUIREMENTS:
#----------------------------------------------------------------------#
env SYNCTHING_EXE="/home/simon/apps/linux/bin/syncthing"
env SYNCTHING_CONF="/home/simon/.config/syncthing"
#----------------------------------------------------------------------#
# set the name of the application
description "Syncthing"
# when Upstart should start syncthing - so when you login to your desktop
start on desktop-start
# when Upstart should stop syncthing - so when you logout to your desktop
stop on desktop-end
# If Upstart detects syncthing has failed - it should restart it
respawn
# the syncthing command Upstart is to execute when it is started up
exec $SYNCTHING_EXE -no-browser -home="$SYNCTHING_CONF"
2 Likes