Node js start and stop windows services

那年仲夏 提交于 2019-12-03 07:30:24

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/23726759/node-js-start-and-stop-windows-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!