如何通知Windows服务已启动?

时间:2018-09-07 20:46:05

标签: node.js windows-services

我正在运行与pkg npm模块打包为exe二进制文件的nodejs HTTP服务器。我需要将其作为Windows服务运行。正常启动时,它可以完美运行。但是,如果我将其作为Windows服务运行,则会发生以下情况:

  • 我开始服务。
  • Windows尝试启动服务。
  • 这段时间内,HTTP服务器是可访问的,并且运行正常
  • Windows服务在30秒后超时,并显示错误消息:“该服务未及时响应启动或控制请求。”

在我看来,我必须以某种方式通知Windows我的服务已启动,让我保持打开状态。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

常规应用程序不能用作Windows服务。作为the reference states,实现应满足某些要求,

  

服务程序必须包括的服务控制管理器(SCM)的接口要求:

     

服务入口点

     

服务ServiceMain功能

     

服务控制处理程序功能

os-service软件包,允许安装启动Node.js脚本的服务。默认情况下,当前脚本被视为入口点:

const osService = require('os-service');

const [action] = process.argv.slice(2);

function errorHandler(err) {
  if (!err) return;
  console.error(err);
  process.exit(1);
}

if (action === '--install') {
  osService.add('Foo', errorHandler);
} else if (action === '--uninstall') {
  osService.remove('Foo', errorHandler);
} else {
  // report service as running
  osService.run('Foo', () => {
    osService.stop();
  });

  // app entry point
}
相关问题