Syncthing: create an Upstart Script on Ubuntu 14.04

Create and open the Upstart file with root privileges in your editor by typing:

sudo nano /etc/init/syncthing.conf

Inside, we will use the following lines to control our Upstart process:

description "Syncthing P2P sync service"

start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [!2345]

env STNORESTART=yes
env HOME=/var/opt/gitlab
setuid "git"
setgid "git"

exec /usr/bin/syncthing

respawn

We start with a general description of the service. Next, we define when the service should start and stop. Here, we are telling the system that it should be started after the filesystem and the non-local networking interfaces are brought up. It will be stopped whenever the system leaves one of the normal runlevels. Next, we will set an environmental variable that tells Syncthing not to restart using its own mechanisms. This is because it actually spawns a new process each time without cleaning up the old one. To avoid this, we will implement the ability to restart within this Upstart script later on. The next environmental variable sets the home directory that will be used by Syncthing. This will be used to find the correct configuration files and pre-populate fields in the GUI. We will also specify the user and group using the setuid and setgid parameters respectively. Modify all three of these values to point to your user's information. Next, we set the path to the actual command that we are going to execute. Finally, we use respawn to tell Upstart to automatically restart the process if it is stopped prematurely. This is used to automatically restart the process (cleanly) when the Syncthing process tries to restart itself internally. When you are finished, save and close the file. You can now start the service by typing:

sudo initctl start syncthing

Source: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-syncthing-to-synchronize-directories-on-ubuntu-14-04

Content type