繁体   English   中英

如何将 node.js 安装为 Windows 服务?

[英]How to install node.js as windows service?

我已经下载了node.js可执行文件。 如何将该可执行文件作为 Windows 服务运行? 我不能使用标准的 node.js 安装程序,因为我需要同时运行多个版本的 node.js。

聚会迟到了,但节点窗口也能做到这一点。

在此处输入图片说明

它还内置了系统日志记录。

在此处输入图片说明

有一个 API 可以从代码创建脚本,即

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\\path\\to\\helloworld.js'
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

svc.install();

FD:我是这个模块的作者。

我发现它非常有用,以至于我围绕它构建了一个更易于使用的包装器( npmgithub )。

安装它:

npm install -g qckwinsvc

安装您的服务:

qckwinsvc

prompt: Service name: [name for your service]
prompt: Service description: [description for it]
prompt: Node script path: [path of your node script]
Service installed

卸载您的服务:

qckwinsvc --卸载

prompt: Service name: [name of your service]
prompt: Node script path: [path of your node script]
Service stopped
Service uninstalled

WinSer是一个对流行的NSSM(Non-Sucking Service Manager)友好的 node.js 包装

我不是直接解决这个问题,而是提供一种替代方案,它也可能以更 node.js 的方式满足您的要求。

功能上的要求是:

  1. 让逻辑(应用程序)在后台运行
  2. 能够启动/停止逻辑
  3. 系统启动时自动启动逻辑

这些要求可以通过使用进程管理器 (PM) 并在系统启动时启动进程管理器来满足。 两个对 Windows 友好的优秀 PM 是:

要让 PM 自动启动,最简单的方法是创建一个带有“At Startup”触发器的计划任务:

在此处输入图片说明

从这个博客

接下来,我想将节点作为服务托管,就像 IIS 一样。 这样它会在我的机器上启动,在后台运行,如果它崩溃会自动重启等等。

这就是nssm ,非吮吸服务管理器,进入图片的地方。 此工具可让您将普通 .exe 作为 Windows 服务托管。

以下是我用来将您的节点应用程序的实例设置为服务的命令,像管理员一样打开您的 cmd 并键入以下命令:

 nssm.exe install service_name c:\\your_nodejs_directory\\node.exe c:\\your_application_directory\\server.js net start service_name

https://nssm.cc/服务助手适用于通过我从 nssm 使用的批处理文件创建 Windows 服务,并且适用于任何应用程序和任何文件

我一年前发布的流程管理器 + 任务调度程序方法适用于一些一次性服务安装。 但最近我开始以微服务的方式设计系统,许多小服务通过 IPC 相互通信。 于是手动配置每个服务就变得难以忍受了。

为了实现无需手动配置即可安装服务的目标,我创建了serman ,这是一个命令行工具(使用npm i -g serman安装)以将可执行文件安装为服务。 您需要编写的所有内容(并且只编写一次)是一个简单的服务配置文件以及您的可执行文件。 跑步

serman install <path_to_config_file>

将安装服务。 stdoutstderr都被记录了下来。 有关更多信息,请查看项目网站

工作配置文件非常简单,如下所示。 但它也有许多有用的特性,例如下面的<env><persistent_env>

<service>
  <id>hello</id>
  <name>hello</name>
  <description>This service runs the hello application</description>

  <executable>node.exe</executable>

  <!-- 
       {{dir}} will be expanded to the containing directory of your 
       config file, which is normally where your executable locates 
   -->
  <arguments>"{{dir}}\hello.js"</arguments>

  <logmode>rotate</logmode>

  <!-- OPTIONAL FEATURE:
       NODE_ENV=production will be an environment variable 
       available to your application, but not visible outside 
       of your application
   -->
  <env name="NODE_ENV" value="production"/>

  <!-- OPTIONAL FEATURE:
       FOO_SERVICE_PORT=8989 will be persisted as an environment
       variable machine-wide.
   -->
  <persistent_env name="FOO_SERVICE_PORT" value="8989" />
</service>

由于qckwinsvc有一段时间没有更新,有一个名为qckwinsvc2 ( npmgithub ) 的新版本

它现在支持传递给服务的参数。 它还保留本地缓存,因此您不必每次要执行操作时都提供路径

安装后立即使用now arg 启动服务

qckwinsvc2 install name="Hello" description="Hello World" path="C:\index.js" args="--y" now
qckwinsvc2 uninstall name="Hello"
qckwinsvc2 list

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM