Skip to content

Instantly share code, notes, and snippets.

@a5ync
Created April 20, 2016 01:04
Show Gist options
  • Select an option

  • Save a5ync/a63c5238b4b1e6e93727703d9bd95d57 to your computer and use it in GitHub Desktop.

Select an option

Save a5ync/a63c5238b4b1e6e93727703d9bd95d57 to your computer and use it in GitHub Desktop.
Powershell wrapper over NSSM (Non-Sucking Service Manager), to install Node app as a Windows-Service
#please read the NSSM https://nssm.cc/usage
Function Install-Service {
param(
[Parameter(Mandatory=$true)][string]$NSSMPath,
[Parameter(Mandatory=$true)][string]$serviceName ,
[Parameter(Mandatory=$true)][string]$serviceExecutable ,
[Parameter(Mandatory=$false)][string]$serviceExecutableArgs ,
[Parameter(Mandatory=$false)][string]$serviceAppDirectory ,
[Parameter(Mandatory=$true)][string]$serviceErrorLogFile ,
[Parameter(Mandatory=$true)][string]$serviceOutputLogFile ,
[Parameter(Mandatory=$true)][string]$serviceUserAccount ,
[Parameter(Mandatory=$true)][string]$serviceUserPassword
)
Write-Host Installing service $serviceName -foreground "green"
Write-Host "NSSM path"+$NSSMPath
Write-Host $serviceName
Write-Host $serviceExecutable
Write-Host $serviceExecutableArgs
Write-Host $serviceAppDirectory
Write-Host $serviceErrorLogFile
Write-Host $serviceOutputLogFile
Write-Host $serviceUserAccount
Write-Host $serviceUserPassword
# install or reinstall your APP as windows service
#
# $NSSMPath = "c:\"
# $serviceName = "YourNSSMService"
# $serviceExecutable ="c:\Program Files\nodejs\node.exe"
# $serviceExecutableArgs ="c:\YourNSSMService\bin\www"
# $serviceErrorLogFile = "c:\YourNSSMService\server.error.log"
# $serviceOutputLogFile = "c:\YourNSSMService\server.out.log"
# $serviceUserAccount= ".\test"
# $serviceUserPassword= "yourSuperSecretPassword"
#################################################################
push-location
Set-Location $NSSMPath
$service = Get-Service $serviceName -ErrorAction SilentlyContinue
if($service)
{
Write-host service $service.Name is $service.Status
Write-Host Removing $serviceName service
&.\nssm.exe stop $serviceName
&.\nssm.exe remove $serviceName confirm
}
Write-Host Installing $serviceName as a service
&.\nssm.exe install $serviceName $serviceExecutable $serviceExecutableArgs
&.\nssm.exe set $serviceName AppStderr $serviceErrorLogFile
&.\nssm.exe set $serviceName AppStdout $serviceOutputLogFile
# setting app directory
if($serviceAppDirectory)
{
Write-host setting app directory to $serviceAppDirectory -foreground "green"
&.\nssm.exe set $serviceName AppDirectory $serviceAppDirectory
}
# setting user account
&.\nssm.exe set $serviceName ObjectName $serviceUserAccount $serviceUserPassword
#settings of
&.\nssm.exe set $serviceName AppStdoutCreationDisposition 2
&.\nssm.exe set $serviceName AppStderrCreationDisposition 2
#start service right away
&.\nssm.exe start $serviceName
pop-location
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment