节点js启动和停止Windows服务

ste*_*425 11 windows-services node.js

我有一个nodeJS应用程序,它与作为Windows服务安装的第三方应用程序进行通信.我的nodeJS应用程序需要运行此服务,但是在某些情况下它可能不会运行.

我试图搜索一种方法来检查这个Windows服务是否正在运行,如果没有启动它.经过多天搜索,我发现许多结果用于运行nodeJS应用程序作为Windows服务,但没有提供启动/停止已安装的Windows服务的能力.

这甚至可能吗?我找到了像PSEXEC这样的工具,所以我可以让nodeJS运行这样的脚本,但如果nodeJS可以原生地执行这个任务,那将是更好的选择.

为此目的提供的任何信息都非常有用,我发现很难相信其他人也不会出现需要这样做的情况.

斯蒂芬

sab*_*ram 10

在Windows中,您可以从命令行键入:

# Start a service
net start <servicename>

# Stop a service
net stop <servicename>

# You can also pause and continue
Run Code Online (Sandbox Code Playgroud)

因此,简单的答案是 - 使用子进程在服务器启动时启动服务.像这样的东西:

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.
});
Run Code Online (Sandbox Code Playgroud)

编辑:我还发现这个nodejs模块叫做" windows-service ".似乎很有希望你正在尝试做什么.它的一些功能是在C++中实现的,它尝试查询Windows服务管理器.

  • Windows服务模块没有用于控制其他服务的API,它仅启用将当前的node.js模块作为服务运行。 (2认同)