TopShelf install multiple of the same service on the same machine - topshelf

I am trying to create windows service using TopShelf. Everything works fine with one instance of the service. However, when I copy the whole service folder to a different location and then run the installation at the location it just hangs on "startup".
I assign the servicename, description, displayaname based on the value in a config files so there is no naming conflict.

It's the service's instancename that you need to differentiate.
From the documentation:
service.exe [verb] [-option:value] [-switch]
install Installs the service
-instance An instance name if registering the service multiple times
So you could use:
service.exe install -instance:FirstInstanceOfMyService
service.exe install -instance:SecondInstanceOfMyService

If what you want is to set the service instance name in the config file, you can set the instance name programatically like this:
var instanceName = ConfigurationManager.AppSettings["Instance"];
HostFactory.Run(hostConfigurator =>
{
...
hostConfigurator.SetDisplayName("My service");
hostConfigurator.SetDescription("My service that does something");
hostConfigurator.SetServiceName("MyService");
hostConfigurator.SetInstanceName(instanceName);
}
So, during the installation you only run
MyService.exe install

Related

InfluxDBv2 - get a remote config working as a telegraf windows service

I'm trying to use the remote config feature of InfluxDB and Telegraf. If I set the enviroment variable with
$env:INFLUX_TOKEN = "thisIsMyToken"
and then test things with
.\telegraf.exe --config "https://influxdbserver:port/myremoteconfig" --test
it seems to be working.
When I try to install the service with
.\telegraf.exe --service install --config "https://influxdbserver:port/myremoteconfig"
the connection does not work and according to the windows event manager it can't load the config file due to "401 Unauthorized". Which is the same error I get when not setting the token before using the --test command.
I assume the problem is with the service not seeing the enviroment variable. But how can I get the variable into the service?
You can use setx, though that'll set an env var that all users on your system can view
setx /M INFLUX_TOKEN "ThisisMyToken"
credits to kittenless_tootler

Installing Windows service with parameters

I have a series of basically identical custom services, which needs to be started with different parameters. The parameters specify which ports to use to communicate with an application, and they all need different ports. I install the services with a batch script:
cd %~dp0\InterfaceService_0x1C01
installutil interface.exe
cd %~dp0\InterfaceService_0x1C02
installutil interface.exe
Then start them like so:
cd %~dp0\InterfaceService_0x1C01
net start Interface0x1C01 /-ip=127.0.0.1 /-pn=32790 /-sp=32791
cd %~dp0\InterfaceService_0x1C02
net start Interface0x1C02 /-ip=127.0.0.1 /-pn=32792 /-sp=32793
It works fine, they all start up on different ports. But when I restart the computer, they start automatically without parameters. I've tried to modify the install script:
cd %~dp0\InterfaceService_0x1C01
installutil Interface.exe
sc config Interface0x1C01 binPath="C:\Program Files (x86)\InterfaceServices\InterfaceService_0x1C01\Interface.exe /-ip=127.0.0.1 /-pn=32790 /-sp=32791"
cd %~dp0\InterfaceService_0x1C02
installutil Interface.exe
sc config Interface0x1C01 binPath="C:\Program Files (x86)\InterfaceServices\InterfaceService_0x1C02\Interface.exe /-ip=127.0.0.1 /-pn=32792 /-sp=32793"
I've also tried without the slashes, but in both cases the service starts up on the default port.
Can someone help with this? I'd like this approach to work, but I could also accept help with installing them set to not start automatically and having a script start them automatically on startup - provided that the user would not need admin privileges at that point.

Windows service using nssm is working but not working with windows SC

I was supposed to convert a GoLang compiled file .exe as a service in windows but as the service was executed with 9 parameters from outside so I decided to use SC to make the .exe as a service and my syntax was ,
sc create myservice binPath= "\"PATH\file.exe\" -param1=value -param2=value -param3=value...-param9=value" displayname= "MyServer" start= auto
the service created successfully but when I try to start it, it fails with
"service did not respond in a timely fashion" ..
But When I created the same service with nssm syntax ,
nssm install myservice "PATH\file.exe" -param1=value -param2=value -param3=value...-param9=value
It was working and I was getting response from my service , I don't know whether the problem is with the syntax in SC or my service...
I even tried many possible ways like removing \" and giving parameters directly like binPath= "PATH/file.exe -param1=value -param2=value...param9=value"
but it didn't work and I also tried to pass the parameters inside quotes , It didn't work either :( Any help would be appreciated.
While SC will happily install any executable as a windows service, it should only be used to install executables that are already Windows Services. If you use SC to install a regular exe, your service will fail with Error 1053 when you try to start it. Your Go exe, which does not implement the Windows Service interface, falls victim to this situation.

node.js http server as a Windows service

I created a simple http server in Node.js.
I wanted to make it run permanently on my Windows 2008 machine, so that, if the computer reboots, it automatically restarts.
So I made it a service with this command:
C:\Users\Administrator>sc create translate binPath= "node D:\Apps\translate\machine-learning-server\servertranslate.js" DisplayName= "Translation Server"
Then started it with:
C:\Users\Administrator>sc start translate
and got the following error message:
[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.
The program works OK when I start it from the command line (not as a service).
What is the easiest way to have a node.js web server that restarts automatically when the computer reboots?
In the past, I've used NSSM for running Node.js applications as services on Windows. It works quite well, and can be configured to automatically restart your application in the event of a crash.
http://nssm.cc/usage
nssm install YourService "C:\Program Files\Node.js\node.exe" "C:\something\something.js"
As I recall, the Service runtime environment isn't the same as running something under the command shell. In particular, Services are required to respond to messages from the system to indicate their running status, as you've seen :-)
This must be a solved problem, though...
Sure enough:
https://npmjs.org/package/windows-service
windows-service
Run Node.JS programs as native Windows Services.
npm install windows-service
Use this one, really simple
https://github.com/coreybutler/node-windows
Create two js file on your project. And run those as
node your_service.js
node your_service_remove.js
For install:
/**
* Created by sabbir on 08/18/2015.
*/
//ref: https://github.com/coreybutler/node-windows
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'nodeDemoApp',
description: 'The nodejs.org example web server.',
script: 'D:\\NodeJS\\demoWeb\\bin\\www'
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
svc.install();
For uninstall:
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'nodeDemoApp',
script: require('path').join(__dirname,'bin\\www')
});
// Listen for the "uninstall" event so we know when it's done.
svc.on('uninstall',function(){
console.log('Uninstall complete.');
console.log('The service exists: ',svc.exists);
});
// Uninstall the service.
svc.uninstall();
At a guess, I'd say that the service doesn't know where to find the node binary. You've probably updated your profile's PATH variable. My recommendation is to ALWAYS hard code the full path in service scripts.
As mentioned in others questions about it, I'd like to share here (because it wasn't referred yet) a node.js module called WinSer that wraps NSSM and its usage is very simple, maybe it helps someone someday.
: )
You could try the package qckwinsvc. First install it globally:
npm install -g qckwinsvc
And then from the cmd:
qckwinsvc
prompt: Service name: [...]
prompt: Service description: [...]
prompt: Node script path: [/path/to/.js file]
To uninstall:
qckwinsvc --uninstall
Always a good idea to look at the number of downloads something is getting.
PM2 seems to be winning and is very easy.
https://medium.com/#harshamw/deploying-a-node-js-application-in-iis-using-a-reverse-proxy-process-management-using-pm2-3d59b83d7f76
You'll need to then use https://www.npmjs.com/package/pm2-windows-service to start it as a windows service on reboot.

How to uninstall a Windows Service when there is no executable for it left on the system?

How do I uninstall a Windows Service when there is no executable for it left on the system? I can not run installutil -u since there is not executable left on the system. I can still see an entry for the service in the Services console.
The reason for this state is probably because of a problem in the msi package that does not remove the service correctly, but how do I fix it once the service is in this state?
You should be able to uninstall it using sc.exe (I think it is included in the Windows Resource Kit) by running the following in an "administrator" command prompt:
sc.exe delete <service name>
where <service name> is the name of the service itself as you see it in the service management console, not of the exe.
You can find sc.exe in the System folder and it needs Administrative privileges to run. More information in this Microsoft KB article.
Alternatively, you can directly call the DeleteService() api. That way is a little more complex, since you need to get a handle to the service control manager via OpenSCManager() and so on, but on the other hand it gives you more control over what is happening.
Remove Windows Service via Registry
Its very easy to remove a service from registry if you know the right path. Here is how I did that:
Run Regedit or Regedt32
Go to the registry entry "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services"
Look for the service that you want delete and delete it. You can look at the keys to know what files the service was using and delete them as well (if necessary).
Delete Windows Service via Command Window
Alternatively, you can also use command prompt and delete a service using following command:
sc delete
You can also create service by using following command
sc create "MorganTechService" binpath= "C:\Program Files\MorganTechSPace\myservice.exe"
Note: You may have to reboot the system to get the list updated in service manager.
Here is the powershell script to delete a service foo
$foo= Get-WmiObject -Class Win32_Service -Filter "Name='foo'"
$foo.delete()
found here
I just tried on windows XP, it worked
local computer:
sc \\. delete [service-name]
Deleting services in Windows Server 2003
We can use sc.exe in the Windows Server 2003 to control services, create services and delete services. Since some people thought they must directly modify the registry to delete a service, I would like to share how to use sc.exe to delete a service without directly modifying the registry so that decreased the possibility for system failures.
To delete a service:
Click “start“ - “run“, and then enter “cmd“ to open Microsoft Command Console.
Enter command:
sc servername delete servicename
For instance, sc \\dc delete myservice
(Note: In this example, dc is my Domain Controller Server name, which is not the local machine, myservice is the name of the service I want to delete on the DC server.)
Below is the official help of all sc functions:
DESCRIPTION:
SC is a command line program used for communicating with the
NT Service Controller and services.
USAGE:
sc
My favourite way of doing this is to use Sysinternals Autoruns application. Just select the service and press delete.
I'd use PowerShell for this
Remove-Service -Name "TestService"
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-service
Create a copy of executables of same service and paste it on the same path of the existing service and then uninstall.

Resources