Last active
May 21, 2025 12:37
-
-
Save pcrockett-pathway/76f5cbdd578c17ef2a12a804959f2060 to your computer and use it in GitHub Desktop.
Install Syncthing as a service using NSSM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Installs Syncthing as a Windows service | |
.DESCRIPTION | |
Uses NSSM (Non-Sucking Service Manager) to set up Syncthing as a Windows | |
service. Requires both Syncthing and NSSM to already be installed. nssm.exe | |
must be available via the PATH environment variable. | |
.PARAMETER InstallDir | |
The Syncthing installation directory where you can find syncthing.exe | |
.PARAMETER LogPath | |
The location where you want to save log files. Automatic log rotation is | |
set up when log file size hits 512 KB. If not specified, defaults to the | |
installation directory under the "log" folder. | |
.PARAMETER ServiceName | |
Defaults to "Syncthing" | |
.EXAMPLE | |
.\Install-SyncthingService.ps1 C:\Syncthing | |
Installs the Syncthing executable at C:\Syncthing\syncthing.exe as a Windows service. | |
.NOTES | |
MIT License | |
Copyright (c) 2019 Pathway Services Inc. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
#> | |
#Requires -RunAsAdministrator | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True)] | |
[string]$InstallDir, | |
[Parameter()] | |
[string]$LogPath, | |
[Parameter()] | |
[string]$ServiceName = "Syncthing" | |
) | |
$ErrorActionPreference = "Stop" | |
Set-StrictMode -Version 4.0 | |
if (!(Get-Command "nssm" -ErrorAction SilentlyContinue)) { | |
throw "nssm.exe not found. Make sure it is included in the PATH environment variable." | |
} | |
function Invoke-Nssm { | |
nssm $args | |
if ($LASTEXITCODE -ne 0) { | |
throw "nssm exited with code $LASTEXITCODE" | |
} | |
} | |
$syncthingExe = Join-Path $InstallDir "syncthing.exe" | |
if (!(Test-Path $syncthingExe)) { | |
throw "$syncthingExe not found." | |
} | |
$homeDir = Join-Path $env:LOCALAPPDATA "Syncthing" | |
if (!$LogPath) { | |
$LogPath = Join-Path $InstallDir "log\log.txt" | |
New-Item -Type File $LogPath -Force | |
} | |
$serviceCreds = Get-Credential -Message "Enter credentials under which the Syncthing service should run, or press cancel to run as LOCALSYSTEM (not recommended)." | |
Invoke-Nssm install $ServiceName $syncthingExe | |
Invoke-Nssm set $ServiceName AppParameters "-no-restart -no-browser -home=""$homeDir""" | |
Invoke-Nssm set $ServiceName AppDirectory $InstallDir | |
Invoke-Nssm set $ServiceName AppExit "Default" "Exit" | |
Invoke-Nssm set $ServiceName AppExit 1 "Restart" | |
Invoke-Nssm set $ServiceName AppExit 2 "Restart" | |
Invoke-Nssm set $ServiceName AppExit 3 "Restart" | |
Invoke-Nssm set $ServiceName AppExit 4 "Restart" | |
Invoke-Nssm set $ServiceName AppStdout $LogPath | |
Invoke-Nssm set $ServiceName AppStderr $LogPath | |
Invoke-Nssm set $ServiceName AppStopMethodConsole 10000 | |
Invoke-Nssm set $ServiceName AppStopMethodWindow 10000 | |
Invoke-Nssm set $ServiceName AppStopMethodThreads 10000 | |
Invoke-Nssm set $ServiceName AppRotateFiles 1 | |
Invoke-Nssm set $ServiceName AppRotateOnline 1 | |
Invoke-Nssm set $ServiceName AppRotateBytes 524288 | |
Invoke-Nssm set $ServiceName DisplayName $ServiceName | |
Invoke-Nssm set $ServiceName Start SERVICE_DELAYED_AUTO_START | |
Invoke-Nssm set $ServiceName Type SERVICE_WIN32_OWN_PROCESS | |
if ($serviceCreds) { | |
# This is the simplest way to get the plain-text password that I'm aware of: | |
$serviceCreds = New-Object System.Net.NetworkCredential ` | |
-ArgumentList $serviceCreds.UserName, $serviceCreds.Password | |
$plaintextPassword = $serviceCreds.Password | |
Invoke-Nssm set $ServiceName ObjectName $serviceCreds.UserName $plaintextPassword | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment