pm2方式管理进程

4.pm2方式管理进程

要保证后台保持进程,需要安装pm2。
全局安装:npm install -g pm2

启动:pm2 start /usr/local/my-yapi/vendors/server/app.js --watch -i 1

这样以后就可以很方便的用pm2就行启动关闭啦~

5. 通过pm2 save保存当前进程状态。

查看:pm2 list

重启:pm2 restart service/app.js

停止:可以先通过pm2 list获取应用的名字(--name指定的)或者进程id。

  # pm2 stop app_name|app_id
  # pm2 stop /usr/local/my-yapi/vendors/server/app.js


pm2使用参考文档:
http://imweb.io/topic/57c8cbb27f226f687b365636

 

本地测试

[root@VM-16-14-centos my-yapi]# npm install pm2 -g
npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
/usr/local/node/bin/pm2 -> /usr/local/node/lib/node_modules/pm2/bin/pm2
/usr/local/node/bin/pm2-dev -> /usr/local/node/lib/node_modules/pm2/bin/pm2-dev
/usr/local/node/bin/pm2-docker -> /usr/local/node/lib/node_modules/pm2/bin/pm2-docker
/usr/local/node/bin/pm2-runtime -> /usr/local/node/lib/node_modules/pm2/bin/pm2-runtime
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.2 (node_modules/pm2/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN ws@7.4.6 requires a peer of bufferutil@^4.0.1 but none is installed. You must install peer dependencies yourself.
npm WARN ws@7.4.6 requires a peer of utf-8-validate@^5.0.2 but none is installed. You must install peer dependencies yourself.

+ pm2@5.2.0
updated 1 package in 12.44s
[root@VM-16-14-centos my-yapi]# pm2 -v
-bash: pm2: command not found
[root@VM-16-14-centos my-yapi]# pm2 start /usr/local/my-yapi/vendors/server/app.js --watch -i 1
-bash: pm2: command not found

执行如下软连接
ln -s /usr/local/node/bin/pm2 /usr/local/bin/

 

[root@VM-16-14-centos bin]# pm2 -v

-------------

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
_\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
_\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
_\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
_\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
_\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
_\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
_\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
_\///______________\///______________\///__\///////////////__


Runtime Edition

PM2 is a Production Process Manager for Node.js applications
with a built-in Load Balancer.

Start and Daemonize any application:
$ pm2 start app.js

Load Balance 4 instances of api.js:
$ pm2 start api.js -i 4

Monitor in production:
$ pm2 monitor

Make pm2 auto-boot at server restart:
$ pm2 startup

To go further checkout:
http://pm2.io/


-------------

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
5.2.0

 
posted @ 2022-08-24 18:22  三驾马车  阅读(546)  评论(0编辑  收藏  举报