pm2 启动node.js
PM2, or Process Manager 2 is an incredibly versatile production process manager written in Node.js.
PM2或Process Manager 2是用Node.js编写的功能强大的生产过程管理器。
PM2 has a lot of uses, let’s look at a few:
PM2有很多用途,让我们看一些:
Restarting after crashes: PM2 allows us to keep processes running until the heat death of the universe, or a server failure, whichever happens first
崩溃后重新启动 :PM2使我们能够保持进程运行,直到Universe热死机或服务器故障(以先发生者为准)为止
Monitoring and managing processes remotely: A magic-powered web portal allows you to keep an eye on remote processes and manage them
远程监控和管理流程 :强大的Web门户使您可以密切关注并管理远程流程
It doesn’t just run Node apps: PM2 isn’t limited to just Node.js processes, that’s right, you can even use it to keep your Minecraft server online
它不仅可以运行Node应用程序 :PM2不仅限于Node.js进程,是的,您甚至可以使用它使Minecraft服务器保持在线
Restart-Persistance: PM2 can remember all your processes and restart them after a system restart
Restart-Persistance :PM2可以记住您的所有进程,并在系统重启后重新启动它们
And a whole lot more
还有更多
First thing we need to do is to install PM2 globally on your machine:
我们要做的第一件事是在您的计算机上全局安装PM2:
$ npm i -g pm2
Let’s get into the basics of how to use it. To start a process under PM2, all you have to do is run pm2 start <app>
. App being the name of the file you’re running. PM2 will output something like this:
让我们了解如何使用它的基础知识。 要在PM2下启动进程,您所需要做的就是运行pm2 start <app>
。 应用是您正在运行的文件的名称。 PM2将输出如下内容:
[PM2] Starting C:\Users\moose\app.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬
│ App name │ id │
├──────────┼────|
│ app │ 0 │
└──────────┴────|
Reduced and simplified for brevity
简化和简化
Pay attention to what it says under id
. If you forget just run pm2 list
. If you want to add a name to the process when you start it, you can use the --name
parameter (pm2 start app.js --name mycoolapp
). Awesome! Your app is now running under PM2, if it crashes, PM2 will restart it.
注意id
。 如果您忘记了,请运行pm2 list
。 如果要在启动过程时为进程添加名称,则可以使用--name
参数( pm2 start app.js --name mycoolapp
)。 太棒了! 您的应用程序现在在PM2下运行,如果崩溃,PM2将重新启动它。
Another awesome feature that PM2 has is restarting when a file in the working directory changes. To make PM2 watch our directory and restart on file changes, include the --watch
flag when you start your app.
当工作目录中的文件更改时,PM2的另一个很棒的功能是重新启动。 要使PM2监视我们的目录并重新启动文件更改,请在启动应用程序时包括--watch
标志。
PM2 also allows us to start our processes when our server (re)starts. Start whatever you want to have running all the time through pm2 start
and then run pm2 save
.
PM2还允许我们在服务器(重新)启动时启动流程。 在pm2 start
一直启动您想要一直运行的内容,然后运行pm2 save
。
$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in C:\Users\moose\.pm2\dump.pm2
Now, when our system restarts, PM2 will start whatever processes we had running when we ran pm2 save
.
现在,当系统重新启动时,PM2将启动运行pm2 save
时运行的任何进程。
PM2 does a lot of things, most of which are out of the scope of this brief introduction. In the future we might cover PM2’s cluster mode, remote process management and more. Thanks for reading!
PM2做很多事情,其中大多数不在本简要介绍的范围之内。 将来,我们可能会介绍PM2的群集模式,远程过程管理等。 谢谢阅读!
翻译自: https://www.digitalocean.com/community/tutorials/nodejs-pm2
pm2 启动node.js