简体   繁体   English

更新 Node.js 后更新 PM2 的正确方法

[英]Proper way to update PM2 after updating Node.js

After updating Node.js from v10.16 to v10.32, PM2 was not detected, however it was running fine when checked with ps aux.将 Node.js 从 v10.16 更新到 v10.32 后,未检测到 PM2,但是使用 ps aux 检查时它运行良好。 Even upon system reboot PM2 functioned correctly even though manual PM2 commands resulted in following type of error.即使在系统重新启动时 PM2 也能正常运行,即使手动 PM2 命令导致以下类型的错误。

pm2 list pm2: command not found pm2 列表 pm2:找不到命令

Switching Node.js back to 10.16 and PM2 commands were again available.将 Node.js 切换回 10.16,PM2 命令再次可用。 fyi PM2 was initially installed under v10.16.仅供参考 PM2 最初安装在 v10.16 下。

While in v10.32 tried PM2 install command npm install pm2 -g but had to use command npm install pm2 -g --unsafe-perm to get operational.虽然在 v10.32 中尝试了 PM2 install 命令 npm install pm2 -g 但必须使用命令 npm install pm2 -g --unsafe-perm 才能运行。

Node.js v10.16 now runs PM2 v10.1. Node.js v10.16 现在运行 PM2 v10.1。 Node.js v10.32 now runs PM2 v10.8. Node.js v10.32 现在运行 PM2 v10.8。

Is this the proper method to keep PM2 versions in sync and working with Node upgrades/changes?这是保持 PM2 版本同步并处理 Node 升级/更改的正确方法吗? Does this need to occur after installing every new version of Node?这是否需要在安装每个新版本的 Node 后发生?

Do not forget to rebuild packages after updating the version of node.js:更新 node.js 版本后不要忘记重新构建包:

cd /to/root/of/your/project
npm rebuild
npm i -g pm2 && pm2 update

# here 0 and dist/main.js change for your project
pm2 delete 0 && pm2 start dist/main.js

When you switch node versions, you also switch the packages, so you need to reinstall pm2 on node update.切换节点版本的同时也切换了包,所以需要在节点更新时重新安装pm2。 Fortunately this does not happen very often.幸运的是,这种情况并不经常发生。

You could make a shell sript to do both in one go.你可以制作一个 shell sript 来一次性完成这两项工作。

For the unsafe-perm thing, it comes only if you install pm2 as root.对于不安全烫发的事情,只有当您以 root 身份安装 pm2 时才会出现。 It makes sense when you think that pm2 has quite a lot of control over your machine's processes.当您认为 pm2 对您机器的进程有很多控制时,这是有道理的。

It's seem there no way without re-installing PM2 after a Node update :-(在节点更新后,如果不重新安装 PM2 似乎没有办法:-(

$ nvm install 6.11.3 --reinstall-packages-from=6.11.2 && nvm alias default 6.11.3
$ nvm uninstall 6.11.2
$ pm2 updatePM2 # Update in memory pm2
$ pm2 startup
$ nano /etc/init.d/pm2-init.sh  # Wrong path :-(

But re-installing pm2 is not enought, some things are still broken even if it seem to work, logs are no more in real time for example My hot fix :但是重新安装 pm2 是不够的,即使它看起来有效,有些东西仍然损坏,日志不再实时,例如我的热修复:

$ rm -rf /root/.pm2
$ pm2 reload pm2.json --env production
$ pm2 startup ubuntu

In console :在控制台中:

  1. pm2 save --First make sure that you saved correctly all your processes pm2 pm2 save首先确保您正确保存了所有进程
  2. npm install pm2 -g --Then install the latest PM2 version from NPM npm install pm2 -g --然后从NPM安装最新的PM2版本
  3. pm2 update --And finally update the in-memory PM2 process pm2 update -兼终于更新内存PM2过程

I tried a lot of times with differnent combinations but still seems not very stable and smart solution.我用不同的组合尝试了很多次,但似乎仍然不是很稳定和智能的解决方案。 Hence I am listing some of the logic I can think of something which may be you can apply and monitor the result as you upgrading and writing the scripts.因此,我列出了一些我能想到的逻辑,您可以在升级和编写脚本时应用和监控结果。

Basically in my situation we have a bunch of applications running under Node .基本上在我的情况下,我们有一堆在Node下运行的应用程序 So things getting complex when you need PM2 to launch anothe application that also installed under Node Version Managers , like NVM所以当你需要PM2来启动另一个安装在Node Version Managers下的应用程序时,事情变得复杂了,比如NVM

Ex.前任。 I have我有

nvm ls
->     v14.17.6

PM2 is installed under: PM2安装在:

which pm2
~/.nvm/versions/node/v14.17.6/bin/pm2

Since I am using App1 (a NodeJS app managed by npm).因为我使用的是 App1(一个由 npm 管理的 NodeJS 应用程序)。 I got:我有:

which App1
~/.nvm/versions/node/v14.17.6/bin/App1

So every time I upgrade using nvm:所以每次我使用 nvm 升级时:

nvm install --lts --reinstall-packages-from=14 --latest-npm

Then nvm using a newer version in this console.然后在此控制台中使用较新版本的 nvm。 eg 14.7.999999例如 14.7.999999

Perhapse I (at most of the time) needs to get my PM2 plus other applications upgrade at the same maintenance window, I use ncu , ncu -g and upgrade them.也许我(大多数时候)需要在同一维护窗口升级我的 PM2 和其他应用程序,我使用ncuncu -g并升级它们。

Now, the applications end up with all new version.现在,应用程序以全新版本结束。 Ex.前任。 a new PM2 instance (local) and an old PM2 running (In Memory) with old comsumer applications (App1) in an old node folder.一个新的 PM2 实例(本地)和一个旧的 PM2 运行(在内存中)与旧节点文件夹中的旧消费者应用程序(App1)。 New version of App1 now exists in new Node application folder but not running.新版本的 App1 现在存在于新的 Node 应用程序文件夹中,但并未运行。

In memory PM2 version: 5.1.0
Local PM2 version: 5.1.1

Anyway, if you don't have an upgraded version of PM2, you probably still looking for a new path of PM2 installed under your new Node folder.无论如何,如果您没有升级版本的 PM2,您可能仍在寻找安装在新 Node 文件夹下的 PM2 的新路径。 If not, you can install PM2 again with the upgraded node如果没有,可以用升级后的节点重新安装PM2

npm i -g pm2

The thing getting worse is PM2 is in system startup scripts which needs to be re-written.更糟糕的是 PM2 位于需要重新编写的系统启动脚本中。 Ex.前任。

/etc/systemd/system/pm2-xx.service /etc/systemd/system/pm2-xx.service

So I endup with vanishing all applications:所以我最终消失了所有的应用程序:

pm2 stop app1 && pm2 delete app1
pm2 stop app2 (verdaccio json startup config) && pm2 delete app2
...
pm2 stop appN && pm2 delete appN

Then do:然后做:

pm2 update

To swap to the new PM2 instance切换到新的 PM2 实例

Then re-provision all the applications然后重新配置所有应用程序

pm2 start app1, app2, ... appN

Then do然后做

pm2 update 

To update the list of applications, check if correct Node path is used.要更新应用程序列表,请检查是否使用了正确的节点路径。

If all applciation paths are corrected Do如果所有应用程序路径都已更正 Do

pm2 startup systemd

And copy and run the suggested Startup Script并复制并运行建议的启动脚本

sudo env PATH=$PATH:/....

Finally run最后运行

pm2 save

To freeze the list on startup.在启动时冻结列表。

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

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