Keeping Syncthing Running: Systemd & regular /etc/init.d

Could anyone tell if the systemd user service works if installed in $XDG_CONFIG_HOME/systemd/user?

Thanks

I just looked a little bit closer and the service file works like this: if pulse/syncthing exists with exit code 0 then systemd will restart it, because it sets environment variable STNORESTART=yes so that pulse/syncthing will not restart but just exit. In my case pulse/syncthing exists with exit code 3, meaning program not running, therefore systemd will not restart it. I fixed it for me by adding SuccessExitStatus=3 to the [Service] of the systemd service file.

I looked at the source code, but I my knowledge of Go ends there :slight_smile: My question would be: What are possible exit codes and why it is not 0 in my case ?

Yeah sure, but you have to start it with: systemctl --user start MYSERVICE. If you want to have it started automatically, use systemctl --user enable MYSERVICE. Read more about it here: https://wiki.archlinux.org/index.php/Systemd/User

It works well for enabling the service and starting it, but upon stopping it enters a failed state. Starting again seems to work well (GUI is OK), but apparently I did something wrong and systemd doesn’t know exactly how to stop the service.

➜  ~  systemctl --user start syncthing 
➜  ~  systemctl --user status syncthing
syncthing.service - Syncthing service for 
   Loaded: loaded (/home/steko/.config/systemd/user/syncthing.service; enabled)
   Active: active (running) since ven 2014-10-17 17:36:41 CEST; 1s ago
 Main PID: 3289 (syncthing)
   CGroup: /user.slice/user-1000.slice/user@1000.service/syncthing.service
           └─3289 /home/steko/Scaricati/syncthing/syncthing

ott 17 17:36:41 ogma systemd[2003]: Starting Syncthing service for ...
ott 17 17:36:41 ogma systemd[2003]: Started Syncthing service for .
➜  ~  systemctl --user is-enabled syncthing
enabled
➜  ~  systemctl --user stop syncthing
➜  ~  systemctl --user status syncthing
syncthing.service - Syncthing service for 
   Loaded: loaded (/home/steko/.config/systemd/user/syncthing.service; enabled)
   Active: failed (Result: exit-code) since ven 2014-10-17 17:38:14 CEST; 2s ago
  Process: 3289 ExecStart=/home/steko/Scaricati/syncthing/syncthing (code=exited, status=2)
 Main PID: 3289 (code=exited, status=2)

ott 17 17:36:41 ogma systemd[2003]: Starting Syncthing service for ...
ott 17 17:36:41 ogma systemd[2003]: Started Syncthing service for .
ott 17 17:38:14 ogma systemd[2003]: Stopping Syncthing service for ...
ott 17 17:38:14 ogma systemd[2003]: syncthing.service: main process exited,...NT
ott 17 17:38:14 ogma systemd[2003]: Stopped Syncthing service for .
ott 17 17:38:14 ogma systemd[2003]: Unit syncthing.service entered failed state.

This means syncthing has stopped with exit code 2. I don’t know what happened, but it says here that there was a Misuse of shell builtins (according to Bash documentation). Maybe a permission problem?

Exit codes are a bit further up the file :wink:

Thanks, I should have searched for exit :smiley:

When STNORESTART=yes is set, Syncthing/Pulse returns 3 as an exit code and Systemd will not restart it unless you add SuccessExitStatus=3 to the service file. A restart from the web gui would not work otherwise.

[Unit]
Description=Syncthing service for %i
After=network.target

[Service]
User=%i
Environment=STNORESTART=yes
ExecStart=/usr/bin/syncthing
Restart=on-success
SuccessExitStatus=3 

[Install]
WantedBy=multi-user.target

I am running OpenSuse 13.1 and I have no experience in systemd services and would like to explain the problems I had when I tried this guide.

Tried multiple different options including copying syncthing.service to systemd/user folder and so on.

$systemctl start syncthing Failed to issue method call

$systemctl --user start syncthing Failed to get D-Bus connection: Did not receive a reply

The solutions to my problem was provided here in proper order:

  1. Copy the systemd service from first post and save as syncthing@.service in folder: /etc/systemd/system/multi-user.target.wants/
  2. $systemctl enable syncthing@$USER.service
  3. $systemctl start syncthing@$USER.service

Now everything seems to be working fine.

Stopping/restarting would be a PITA. Especially the latter.

You’re supposed to run it as sudo systemctl start syncthing@USERNAME, not systemctl --user.

What I just said above applies. Also, the ArchLinux package actually displays a message post-installation telling you exactly this.

I did not miss the install message. Do you know why this is? I would like it better, when this service is controlled by the user itself, that is why I tried to get it working with systemctl --user.

Is there any reason why you can’t just sudo systemctl? The service runs as the user anyway. And it can restart itself once it’s started. (or you can kill it and have systemd restart it for you).

Like I said, as a user space process it should be controlled by the user. The user can not stop or start any services of this type, because the service was set by the super user. Correct if I am wrong but I think that the service is started at system start and therefore can affect the overall performance depending on the number of users using syncthing, repositories and so on. The user service manager (systemctl --user) is started on the first log in of the according user and its logs can only be viewed by the according user (privacy). I know for a single user system there is no difference but for multi user systems it makes sense to use the user service manager and I am always glad to keep my activities in user space :slight_smile:

As I said before, the above script runs the service as the configured your (eg: your user, not root), even if it’s started by root (systemd will change user before starting the process). (I’m assuming that’s what you refer to with “user space”, since syncthing can’t run outside user space).

I do agree on the multi-user point; syncthing may degrade performance on shared hosts or have other undesired effects. Just also keep in mind that by running with systemd --user syncthing will not sync files until you log in on more than one host simultaneously.

1 Like

If you use a package, then the systemd script comes ready to use (for openSUSE)

See https://pulse-forum.ind.ie/t/opensuse-package-for-syncthing/189/26

I would like to tweak the init.d script for debian, in order to keep logs in a file. How can I do this ?

1 Like

I have debian, used this init.d script mixed with this installation guide. Result is that I have syncthing running as a dameon/service by user syncthing. It was working ok for few days, but now I cannot connect to web gui… when I go to my IP:port there is no response, it jsut loads forever.

The syncthing pseems to be running because:

syncthing@cedrus:$ sudo service syncthing status
syncthing for USER syncthing: running (pid 2268024987)

It looks like for whatever reason the /etc/init/syncthing.conf is missing… there is no such file…

Any ideas what went wrong and how to correct it?

To reply to myself.

I find out that the syncthing confing is actually in home directory ./config/syncthing/config.xml

there in the config in the gui section was some “> api key” which i dont remember setting, so I removed it

Then for whatever reason the service syncthing status was returning 6 processes, so I killed them all, and restarted as service syncthing start

and it works again…

wonder why this all happened…

I am evaluating syncthing as a replacement for my dropbox, but so far it is too much geeky, expecially the server part (syncthing running as daemon on my little debian box)…

A light modified version of the debian init.d script can be found here: http://download.prodigy7.de/files/programs/any/syncthing/syncthing Output from stdout are redirected to logfile in /var/log. Please add init script to package.

Please make a pull request.