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
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.