Salta al contenuto principale


I was puzzled on why #syncthing on my laptop was showing "2021-06-02" as last synchronization date with my #raspberrypi.
So I logged in my raspi via ssh, and magically the two devices started to sync.
A quick look at the raspberrpi's journal (# journalctl --since=today) showed:

ago 02 17:22:20 raspberrypi systemd[32888]: pam_unix(systemd-user:session): session opened for user pi(uid=1000) by (uid=0)
[...]
ago 02 17:22:21 raspberrypi syncthing[32908]: [start] INFO: syncthing v1.12.1-ds1 "Fermium Flea" (go1.15.9 linux-arm64) debian@debian 2021-04-08 21:52:00 UTC

🤦
I had setuped Syncthing to start with a user #systemd service unit, so the service was running only when user had log in and was stopped when the user logs out!

I want the synced files to be own by the user, so I have to run Synchthing from a system unit, but as my user. Now.. I could copy the user service file and set up a system service file from there but.... the kind developers of Syncthing have done that for us already!

It's been a matter of

$ systemctl --user disable syncthing.service --now
$ sudo systemctl enable synching@pi.service --now

where pi is the user Synching will run as... nice!

in reply to Fabio

@Fabio why was it running only when you login when you setup syncthing.service and not syncthing$user.service ? I don't get that part.
in reply to utzer [Friendica]

@Fabio I usually setup Syncthing as a user, mostly as my main user, but I don't get the problem above.
in reply to utzer [Friendica]

problem is that user's service units are run by systemd's user session, that it is created when user logs in and removed when user logs out, stopping all user units. But my raspberrypi is running as a home server, so no user is logged in usually. I want synching on raspberry running all the time. So I need to run it from a system unit instead of a user one.
So when it is setup as

pi@rpi$ systemctl --user enable synching.service

that is my pi user enabling the syncthing user's service for himself.
That's what you want on a desktop or a laptop: when you log into you desktop, synchting is started automatically.

when instead is setup like this

pi@rpi$ sudo systemctl enable syncthing@pi.service

this is root (via sudo) setting a service unit for syncthing, using a template unit file: the part you set after the @ when you enable the service is passed as argument, which in unit file is used as a "variable" called %i:

This is part of the unit file on disk, called syncthing@.service:

[...]
[Service]
User=%i
ExecStart=/usr/bin/syncthing serve --no-browser --no-restart --logflags=0
Restart=on-failure
[...]

There the argument set the user which will be used to run the service.

Systemd will run syncthing at boot (after network is set up) as my user pi, whitout the need of an entire user session for it.

I hope all of this make some sense :P

in reply to Fabio

@Fabio this make sense, my problem was I did not even know there is the --user option for systemctl.

pi@rpi$ systemctl --user enable synching.service

Never came across this argument. So this would run a service only if a user is logged in on any TTY or CLI or graphical screen?

Questa voce è stata modificata (3 anni fa)
in reply to utzer [Friendica]

yup. is a systemd session started from scratch by pam when the user logs in for the first time.

see wiki.archlinux.org/title/Syste…

Questo sito utilizza cookie per riconosce gli utenti loggati e quelli che tornano a visitare. Proseguendo la navigazione su questo sito, accetti l'utilizzo di questi cookie.