Starting syncthing from systemd

You can find it out easily:

$ systemctl list-unit-files | grep syncthing
$ systemctl cat YOUR-SERVICE.service

Weird… there is not syncthing!

How have you installed syncthing?

With deb package

IIRC the deb package does not include the service files yet. Is this correct @calmh? You could instead follow the instructions in the wiki, should be fairly easy to setup. Something like this:

#!/usr/bin/env bash

userunits=~/.config/systemd/user
mkdir -p $userunits
cd $userunits
curl -LO https://raw.githubusercontent.com/syncthing/syncthing/master/etc/linux-systemd/user/syncthing.service

systemctl --user daemon-reload
systemctl --user start syncthing.service

There are no systemd files in the Debian package, no. At least it needs someone systemd-knowledgeable to give a thumbs up or down on the pull request. Possibly we may not even want to include the files in their “real” location anyway, given the package is installable on systems that don’t have systemd and where we maybe shouldn’t create a /usr/lib/systemd either…

Unfortunately I get the same error:

SRVR ~/.config/systemd/system # systemctl --system start syncthing.service
Failed to issue method call: Unit syncthing.service failed to load: Bad message. See system logs and 'systemctl status syncthing.service' for details.
SRVR ~/.config/systemd/system :( # systemctl status syncthing.service
syncthing.service
          Loaded: error (Reason: Bad message)
          Active: active (exited) since Wed, 20 May 2015 23:32:10 +0200; 3min 3s ago

May 20 23:32:09 SRVR syncthing[7966]: Starting Syncthing...
May 20 23:32:10 SRVR syncthing[7966]: 23:32:10 FATAL: No home directory found - set $HOME (or the platform equivalent).

Please show us your service file. That’s not the one I have written, since it is for the system instance without any templating.

$ systemctl --system cat syncthing.service

It might be my fault: I have tried to start the service as root.

SRVR ~/.config/systemd/system # cat syncthing.service
[Unit]
Description=Syncthing - Open Source Continuous File Synchronization
Documentation=https://github.com/syncthing/syncthing/wiki
After=network.target

[Service]
Environment=STNORESTART=yes
ExecStart=/usr/bin/syncthing -no-browser -logflags=0
Restart=on-failure
SuccessExitStatus=2 3 4
RestartForceExitStatus=3 4

[Install]
WantedBy=default.target

Don’t do that. Run it as the user who should own the files.

Thanks for solving my problem!

Can I ask why? I’m just curious.

Running it as root gives it way too much power and opens up for worse problems than necessary in case of misconfiguration or something malicious. Plus your synced will be owned by root, which is usually not the intention?

Again, please follow the docs. You have mixed up things. This service file you use is intended for the user instance. So place it in ~/.config/systemd/user, and start it as the current user with

$ systemctl --user start syncthing.service

When you really want to use it as root, then use the system file. Place it in the appropriate load path, /etc/systemd/system, and type:

# systemctl start syncthing@root.service

But as calmh already outlined, don’t do that…

1 Like

Many thanks to all of you, I understood!

There is only one problem by starting syncthing as a user and not as root: in my case syncthing is running on a headless server thus it won’t start automatically if the user running syncthing is not logged in.

That is probably solvable somehow with systemd (although I know nothing about it), otherwise I would recommend the runit way which is trivial and works fine for the purpose.

1 Like

Thanks again!

You can still use the system file that @rumpelsepp linked above , but just enable and start it as ‘systemctl@giovi.service’. The line ‘User=%i’ will run the command in the service file as the user after the ‘@’ sign.

Scott

1 Like

Exactly. It is even documented in the wiki!

Edit: the link is outdated; here is the most recent one: https://docs.syncthing.net/users/autostart.html