Syncthing as a Service on Windows

I know there exists a thread already in the Support section which offers lots of viable solutions to run Syncthing as service. But all those methods require a user to find that thread here on this forum first. A normal guy won’t do that. I would really request you to work on a release which allows users to create a windows service and wrap it with an installer utility which makes thing easy and seamless. Only then you can expect Syncthing to gain popularity on Windows platform.

Feel free to contribute. You can use syncthing-gtk which has an installer and installs it as a service for the incapable ones.

Also, feel free to add a Wiki page on github.

You can use windows service wrapper Winsw to start as service

Download winsw.exe -> rename it to winswsyncthing.exe

Create file winswsyncthing.xml

<service>
<id>Syncthing</id>
<name>Syncthing</name>
<description>Syncthing service on port 8080</description>
<executable>C:\Program Files\syncthing\syncthing.exe</executable>
<stopexecutable>C:\Program Files\syncthing\stop-syncthing.cmd</stopexecutable>
<!-- <logpath>c:\tmp</logpath> -->
<logmode>roll</logmode>
<startargument>-no-console</startargument>
<startargument>-no-browser</startargument>
</service>

Create file stop-syncthing.cmd

taskkill /f /IM syncthing.exe

Make sure all paths are correct.

Run command where winswsyncthing.xml is (xml and winsw exe must have same name) as root:

winswsyncthing.exe install

And finally Syncthing appears in windows service list and you can start it:

Wouldn’t that taskkill command corrupt the index as it’s forcing it to close?

You can see my solution here. Then it’s just a case of opening services.msc or an elevated command prompt and running the command net stop Syncthing in order to stop Syncthing from running, should you want to.

What does net stop Syncthing really do?

Syncthing should implement parameter syncthing.exe --stop which would nicely shutdown syncthing.

I think net stop Syncthing does something of equivalent of SIGTERM/INT on windows, which allows the process to wind down and clean up.

I was googling for gracefully taskkill in windows, but didn’t find anything useful.

Best way to stop it to call [POST] http://127.0.0.1:8080/rest/shutdown (but it needs authentication)

I’v just find in logs that takkill tries SIGINT, but fails:

2015-03-04 13:57:33 - Send SIGINT 15708
2015-03-04 13:57:33 - SIGINT to 15708 failed - Killing as fallback

If you install Syncthing as a service then net stop Syncthing will shut the service down gracefully.

https://technet.microsoft.com/en-gb/library/cc736564(v=ws.10).aspx

Here is the output from Syncthing running as a service when I use the net stop command:

[monitor] 2015/03/04 13:36:04.089473 monitor.go:130: INFO: Signal 2 received; exiting

net stop calls registered command for service shut down, same as Winsw defines it. If you look at source at https://github.com/bloones/SyncThingWin registered command is StopProgram function which attaches itself to console window of syncthing and sends CTRL-C event.

if (AttachConsole((uint)proc.Id))
{
    SetConsoleCtrlHandler(null, true);
    GenerateConsoleCtrlEvent(CtrlTypes.CTRL_C_EVENT, 0);
    proc.WaitForExit(2000);
    FreeConsole();
    SetConsoleCtrlHandler(null, false);
}

but it would be much simpler if there is some direct external command line for shutdown.

A vote here for this, I am one of the people flocking from bittorrent sync 2, I have people I have shares with that would not have a clue about any of this, the ability to have it running in the background (a tray icon isnt a deal breaker) on their machines without having to do anything else each time they rebooted or updated would convince me to jump ship completely into syncthing. Basically if they don’t know it is there, like for the most part bittorrent sync,that is a success in my book

I’m not quote sure why you’re getting confused here, it IS a graceful shutdown as can be seen from the output of stdout. Syncthing closes itself down after receiving a signal 2.

CTRL+C is the terminate command hence NOT a graceful shutdown.

Ctrl+C is graceful. taskkill is not.

CTRL+C is terminate unless Syncthing has something built in the source to catch a SIGINT and gracefully shutdown.

EDIT: just tested this and the Go library treats it as signal 2 above and shuts Syncthing down gracefully

Based on this idea of the thread for “Syncthing Windows Installer”, I create a project on github called “Syncthing Light”.

Syncthing Light is a simple package installer for install and runs Syncthing as Windows Service (without tray utility - runs hidden).