天天看点

记一次 Centos7.x PM2 启动 node 应用一、基本信息二、简介三、环境、背景四、我的 PM2 一次实战五、常用命令

记一次 Centos7.x PM2 启动 node 应用一、基本信息二、简介三、环境、背景四、我的 PM2 一次实战五、常用命令

一、基本信息

官网:https://pm2.keymetrics.io/

官方文档:http://pm2.keymetrics.io/docs/usage/quick-start

二、简介

PM2是node进程管理工具,可以利用它来简化很多node应用管理的繁琐任务,如性能监控、自动重启、负载均衡等,而且使用非常简单。

下面就对PM2进行入门性的介绍,基本涵盖了PM2的常用的功能和配置。

三、环境、背景

1、安装配置 node 环境

https://blog.csdn.net/llwy1428/article/details/111880286

2、安装 pm2

# 使用淘宝源进行安装
[[email protected] ~]# npm install -g pm2 --registry https://registry.npm.taobao.org
# 普通安装
[[email protected] ~]# npm install -g pm2
           
记一次 Centos7.x PM2 启动 node 应用一、基本信息二、简介三、环境、背景四、我的 PM2 一次实战五、常用命令

3、目录文件列表

[[email protected] ~]# ll ~/.pm2
总用量 16
drwxr-xr-x 2 root root    6 1月  31 23:53 logs
-rw-r--r-- 1 root root    2 1月  31 23:53 module_conf.json
drwxr-xr-x 2 root root    6 1月  31 23:53 modules
drwxr-xr-x 2 root root    6 1月  31 23:53 pids
-rw-r--r-- 1 root root 1195 1月  31 23:53 pm2.log
-rw-r--r-- 1 root root    4 1月  31 23:53 pm2.pid
srwxrwxr-x 1 root root    0 1月  31 23:53 pub.sock
srwxrwxr-x 1 root root    0 1月  31 23:53 rpc.sock
-rw-r--r-- 1 root root   13 1月  31 23:53 touch
           

4、各目录、文件介绍

$HOME/.pm2 will contain all PM2 related files

$HOME/.pm2/logs will contain all applications logs

$HOME/.pm2/pids will contain all applications pids

$HOME/.pm2/pm2.log PM2 logs

$HOME/.pm2/pm2.pid PM2 pid

$HOME/.pm2/rpc.sock Socket file for remote commands

$HOME/.pm2/pub.sock Socket file for publishable events

$HOME/.pm2/conf.js PM2 Configuration

5、执行查看 pm2 管理的服务列表

记一次 Centos7.x PM2 启动 node 应用一、基本信息二、简介三、环境、背景四、我的 PM2 一次实战五、常用命令

四、我的 PM2 一次实战

1、此处我以私有化部署 YApi 应用来举例

Centos7.x YApi 私有化部署过程

https://blog.csdn.net/llwy1428/article/details/113408974

2、我在控制台启动,都是通过npm start启动应用

记一次 Centos7.x PM2 启动 node 应用一、基本信息二、简介三、环境、背景四、我的 PM2 一次实战五、常用命令

3、换成 pm2 就是

记一次 Centos7.x PM2 启动 node 应用一、基本信息二、简介三、环境、背景四、我的 PM2 一次实战五、常用命令

注意,这里用了–watch参数,意味着当你的express应用代码发生变化时,pm2会帮你重启服务

4、查看监控状态

记一次 Centos7.x PM2 启动 node 应用一、基本信息二、简介三、环境、背景四、我的 PM2 一次实战五、常用命令

5、关闭指定服务

五、常用命令

参数说明:

–watch:监听应用目录的变化,一旦发生变化,自动重启。如果要精确监听、不见听的目录,最好通过配置文件。

-i --instances:启用多少个实例,可用于负载均衡。如果-i 0或者-i max,则根据当前机器核数确定实例数目。

–ignore-watch:排除监听的目录/文件,可以是特定的文件名,也可以是正则。比如–ignore-watch=“test node_modules “some scripts””

-n --name:应用的名称。查看应用信息的时候可以用到。

-o --output

:标准输出日志文件的路径。

-e --error

:错误输出日志文件的路径。

–interpreter :the interpreter pm2 should use for executing app (bash, python…)。比如你用的coffee script来编写应用。

完整命令行参数列表:地址

pm2 start app.js --watch -i 2

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

pm2 stop app_name|app_id

如果要停止所有应用,可以

pm2 stop all

删除

类似pm2 stop,如下

pm2 stop app_name|app_id

pm2 stop all

查看进程状态

pm2 list

查看进程的信息

记一次 Centos7.x PM2 启动 node 应用一、基本信息二、简介三、环境、背景四、我的 PM2 一次实战五、常用命令

常用命令介绍

启动一个 app

启动其他类型的程序

[[email protected]~]# pm2 start bashscript.sh
[[email protected]~]# pm2 start python-app.py --watch
[[email protected]~]# pm2 start binary-file -- --port 1520
           

启动参数说明

# Specify an app name
--name <app_name>
# Watch and Restart app when files change
--watch
# Set memory threshold for app reload
--max-memory-restart <200MB>
# Specify log file
--log <log_path>
# Pass extra arguments to the script
-- arg1 arg2 arg3
# Delay between automatic restarts
--restart-delay <delay in ms>
# Prefix logs with time
--time
# Do not auto restart app
--no-autorestart
# Specify cron for forced restart
--cron <cron_pattern>
# Attach to application log
--no-daemon
           

启动、重启、停止、删除指定服务

$ pm2 start app_name
$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name
           

查看服务详情状态

$ pm2 [list|ls|status]

监控(monitor)

pm2 monit

记一次 Centos7.x PM2 启动 node 应用一、基本信息二、简介三、环境、背景四、我的 PM2 一次实战五、常用命令

升级

npm install [email protected] -g

更新

pm2 update

其他基础命令

# Fork mode
pm2 start app.js --name my-api # Name process
# Cluster mode
pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs
pm2 start app.js -i max      # Same as above, but deprecated.
pm2 scale app +3             # Scales `app` up by 3 workers
pm2 scale app 2              # Scales `app` up or down to 2 workers total
# Listing
pm2 list               # Display all processes status
pm2 jlist              # Print process list in raw JSON
pm2 prettylist         # Print process list in beautified JSON
pm2 describe 0         # Display all informations about a specific process
pm2 monit              # Monitor all processes
# Logs
pm2 logs [--raw]       # Display all processes logs in streaming
pm2 flush              # Empty all log files
pm2 reloadLogs         # Reload all logs
# Actions
pm2 stop all           # Stop all processes
pm2 restart all        # Restart all processes
pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)
pm2 stop 0             # Stop specific process id
pm2 restart 0          # Restart specific process id
pm2 delete 0           # Will remove process from pm2 list
pm2 delete all         # Will remove all processes from pm2 list
# Misc
pm2 reset <process>    # Reset meta data (restarted time...)
pm2 updatePM2          # Update in memory pm2
pm2 ping               # Ensure pm2 daemon has been launched
pm2 sendSignal SIGUSR2 my-app # Send system signal to script
pm2 start app.js --no-daemon
pm2 start app.js --no-vizion
pm2 start app.js --no-autorestart
           

更多请参考官方文档:http://pm2.keymetrics.io/docs/usage/quick-start

至此,Centos7.x PM2 启动 node 应用操作完毕,希望能够对您有所帮助!

继续阅读