注销后保留 node.js 表示 运行

Keeping node.js express running after logoff

我设置了一个 node.js 应用程序,从 pm2 开始,一切正常

我还尝试添加监视和保存选项(请参阅下面有关 windows 服务的更多信息)

pm2 start app.js --watch
pm2 save

但在我从 Windows RDP 注销后,应用程序最终不再 运行。

低于 pm2 report

的相关输出
==
PM2        | 2021-04-10T11:26:39: PM2 log: --- New PM2 Daemon started ----------------------------------------------------
PM2        | 2021-04-10T11:26:39: PM2 log: Time                 : Sat Apr 10 2021 11:26:39 GMT+0200 (Central European Summer Time)
PM2        | 2021-04-10T11:26:39: PM2 log: PM2 version          : 4.5.6
PM2        | 2021-04-10T11:26:39: PM2 log: Node.js version      : 14.16.1
PM2        | 2021-04-10T11:26:39: PM2 log: Current arch         : x64
PM2        | 2021-04-10T11:26:39: PM2 log: PM2 home             : V:\.pm2
PM2        | 2021-04-10T11:26:39: PM2 log: PM2 PID file         : V:\.pm2\pm2.pid
PM2        | 2021-04-10T11:26:39: PM2 log: RPC socket file      : \.\pipe\rpc.sock
PM2        | 2021-04-10T11:26:39: PM2 log: BUS socket file      : \.\pipe\pub.sock
PM2        | 2021-04-10T11:26:39: PM2 log: Application log path : V:\.pm2\logs
PM2        | 2021-04-10T11:26:39: PM2 log: Worker Interval      : 30000
PM2        | 2021-04-10T11:26:39: PM2 log: Process dump file    : V:\.pm2\dump.pm2
PM2        | 2021-04-10T11:26:39: PM2 log: Concurrent actions   : 2
PM2        | 2021-04-10T11:26:39: PM2 log: SIGTERM timeout      : 1600
PM2        | 2021-04-10T11:26:39: PM2 log: ===============================================================================


Please copy/paste the above report in your issue on https://github.com/Unitech/pm2/issues

我想知道 pm2 主路径是否是根本原因,因为它是一个用户共享,但我已经找到如何通过 PM2_HOME env var 在 windows 上更改它并且它也不起作用。

我搜索了 github 个问题,那里似乎已经有类似的未解决问题,例如 this one:

Same happens if I go directly to server start pm2 and logoff user, all processes will be gone. Is there a way to start pm2 on widows globally so it doesn't exit when user is logging out ? Or I am doing something wrong ?

I would also like to understand if there is any option to keep Apps running after logging off the user from Windows serve

这里有人有解决方案吗?

我还尝试按照 readme here.

中的描述使用 admin cmd 设置 pm2-windows-service

并且我在 Express 应用程序中添加了 SIGTERM 的处理程序:

app.listen(HTTP_PORT, () => {
    console.log("Server is listening on port " + HTTP_PORT);
});

process.on('SIGINT', function() {
    console.log('Do not shut down the app on user log-off');
    //server.close();
  });

PM2 主页路径和应用程序路径中有一个奇怪的结尾 space 当我试图在资源管理器中重命名该文件夹时,该文件夹被锁定(多次重新启动后仍然“锁定”...最终使用 this special syntax 删除)。 用下划线更改了 space,我重新启动了虚拟机并发出了

pm2 reloadLogs
pm2 report

最后我重新启动并通过pm2再次保存了应用程序。

pm2 save 之前,请注意在您的 admin 命令提示符中从 pm2-service-install 输出的注释非常重要。

PM2_HOME value (this path should be accessible to the service user and should not contain any "user-context" variables [e.g. %APPDATA%]): F:\Appl\pm2

pm2-service-install 之后,您仍然需要 手动 启动服务 第一次 。此外设置“以本地系统帐户登录”和标记允许服务与桌面交互” .

注销并重新启动虚拟机 仔细检查:pm2 应用程序现在 运行 正常(我必须使用和 admin cmd 虽然是 pm2)。

如果您部署更新需要新环境变量,请使用

刷新变量
pm2 restart 0 --update-env
pm2 save

别忘了在 --update-env 之后 save

使用

检查您的应用(例如 id 0)
pm2 env 0

在这种情况下请记住,您可以通过重新启动 PM2 服务(或者,当然,重新启动计算机)来快速 check/test。

当我遇到类似问题时,我不必使用 pm2 或永远。我做了什么:我使用了以下工具 nssm 到 运行 expressjs 和 feathersjs。即使在 windows 用户注销后,应用仍会保留 运行ning。它还会在系统启动后启动服务。 您还可以使用此工具来 运行 您需要的任何脚本。