如何通知Windows该服务已启动?

问题描述 投票:1回答:1

我正在使用pkg npm模块运行一个打包为exe二进制文件的nodejs HTTP服务器。我需要将其作为Windows服务运行。正常启动时效果很好。但是如果我将它作为Windows服务运行,会发生什么:

  • 我开始服务。
  • Windows尝试启动该服务。
  • 在此期间,HTTP服务器是可访问的,并且工作正常
  • Windows服务在30秒后因错误而超时:“服务未及时响应启动或控制请求。”

在我看来,我不得不告诉Windows我的服务已经开始,让我保持开放。

我怎样才能做到这一点?

node.js windows-services
1个回答
0
投票

常规应用程序无法用作Windows服务。作为the reference states,实施应满足某些要求,

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

服务入口点

服务服务主要功能

服务控制处理程序功能

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
}
© www.soinside.com 2019 - 2024. All rights reserved.