Node js start and stop Windows services

I have a nodeJS application that interacts with a third-party application installed as a Windows service. My nodeJS application requires this service to work, however, if in some cases it is not.

I am trying to find a method to test the health of this Windows service and not start it. After many days of searching, I found a lot of results for starting the nodeJS application as a Windows service, but not for making it possible to start / stop already installed Windows services.

Is it possible? I found tools like PSEXEC, so I could get nodeJS to run such a script, but it would be preferable if nodeJS could complete this task initially.

Any information for this purpose would be very useful, and it is difficult for me to believe that others were not in a situation where they also needed it.

Stephen

+6
source share
1 answer

In windows from the command line you can enter:

# Start a service net start <servicename> # Stop a service net stop <servicename> # You can also pause and continue 

So, the simple answer is to use a child process to start the service when your server starts. Something like that:

 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 ". It seems promising for what you are trying to do. Some of its functions are implemented in C ++, where it tries to request the Windows Service Manager.

+3
source

Source: https://habr.com/ru/post/969477/


All Articles