node 安装 pm2

  1. pm2 安装
    npm install pm2 -g

  2. pm2 的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module.exports = {
apps: [
{
name: "lunbuka-m-h5", // 这个name就是pm2启动时需要的name
exec_mode: "cluster",
instances: "max", // Or a number of instances
script: "./node_modules/nuxt/bin/nuxt.js",
args: "start --mode prod",
out_file: "/data/pm2/logs/lunbuka-m-h5_out.log",
error_file: "/data/pm2/logs/lunbuka-m-h5_error.log",
combine_logs: true,
merge_logs: true,
cwd: "/data/web/kalunbu-m-h5", // absolute path to nuxt dir
max_memory_restart: "512M" // in case nuxt renderer eats all memory, it will be restarted
}
]
};

配置文件更改后,需要 kill pm2, 重新启动才能生效

  1. 日志文件分割
    pm2 install pm2-logrotate

    常用命令
    pm2 set pm2-logrotate:max_size 10K
    pm2 set pm2-logrotate:retain 30
    pm2 set pm2-logrotate:compress false
    pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss
    pm2 set pm2-logrotate:workerInterval 30
    pm2 set pm2-logrotate:rotateInterval 0 0 * * *
    pm2 set pm2-logrotate:rotateModule true

  2. pm2 常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

参考:https://pm2.keymetrics.io/docs/usage/quick-start/