Node js启动和停止windows服务 [英] Node js start and stop windows services
问题描述
我有一个 nodeJS 应用程序,它与作为 Windows 服务安装的第三方应用程序进行通信.我的 nodeJS 应用程序需要运行此服务,但在某些情况下可能不会.
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.
我正在尝试寻找一种方法来检查此 Windows 服务是否正在运行以及是否未启动它.经过多天的搜索,我发现了许多将 nodeJS 应用程序作为 Windows 服务运行的结果,但没有一个能够启动/停止已安装的 Windows 服务.
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.
这甚至可能吗?我找到了像 PSEXEC 这样的工具,所以我可以让 nodeJS 运行这样的脚本,但如果 nodeJS 可以在本地执行这个任务会更好.
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.
斯蒂芬
推荐答案
在 Windows 中,您可以从命令行输入:
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
所以,简单的答案是 - 使用 child-process 在服务器启动时启动服务.像这样:
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.
});
我还发现了这个名为windows-service的nodejs模块".似乎对您正在尝试做的事情很有希望.它的一些功能是用 C++ 实现的,它尝试查询 Windows 服务管理器.
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.
这篇关于Node js启动和停止windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!