Reputation: 4776
I have a nodeJS app that communicates with a third party application installed as a windows service. My nodeJS application requires this service to be running, however if some circumstances it may not.
Im trying to search for a method to check if this windows service is running and if not start it. After many days searching i have found many results for running a nodeJS application as a windows service but not one providing the ability to start/stop already installed windows services.
Is this even possible? I have found tools like PSEXEC so I could make nodeJS run such a script but it would be preferable if nodeJS could perform this task natively.
Any information to this end would be greatly useful and i find it hard to believe others haven't been in a situation where they have needed to do this also.
Stephen
Upvotes: 16
Views: 21986
Reputation: 907
In windows, from a command line you can type:
# Start a service
net start <servicename>
# Stop a service
net stop <servicename>
# You can also pause and continue
So, the simple answer is - use child-process to start the service on startup of your server. Something like this:
var child = require('child_process').exec('net start <service>', function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
// Validate stdout / stderr to see if service is already running
// perhaps.
});
EDIT: I also found this nodejs module called "windows-service". Seems promising for what you are attempting to do. Some of its functionality is implemented in C++ where it attempts to query the Windows Service Manager.
Upvotes: 16