安装
需要先安装 nodejs。
npm install pm2@latest -g
常用命令
pm2 -h //查看帮助文档
pm2 list //列出当前 pm2 管理的所有进程
配置文件
http://pm2.keymetrics.io/docs/usage/application-declaration/
# Start all applications
pm2 start ecosystem.config.js
# Start only the app named worker-app
pm2 start ecosystem.config.js --only worker-app
pm2 start ecosystem.config.js --env production
# Stop all
pm2 stop ecosystem.config.js
# Restart all
pm2 start ecosystem.config.js
## Or
pm2 restart ecosystem.config.js
# Reload all
pm2 reload ecosystem.config.js
# Delete all
pm2 delete ecosystem.config.js
配置文件 ecosystem.config.js
pm2 ecosystem
生成配置文件 ecosystem.config.js
module.exports = {
apps: [
{
// name (string) application name.
name: 'filterServer',
// script (string) script path relative to pm2 start.
script: './filterServer.js',
// cwd (string) the directory from which you app will be launched.
cwd: './',
// args (string) all arguments passed to script.
// args: 'one two',
// interpreter (string) interpreter absolute path (default to node)
interpreter: 'node',
// interpreter_args (string) option to pass to the interpreter.
// interpreter_args: '-harmony',
// instances (number) number of app instance to be launched.
instances: 1,
// true by default. if false, PM2 will not restart your app if it
// crashes or ends peacefully
autorestart: true,
// a cron pattern to restart your app. Application must be running for
// cron feature to work
// cron_restart: '1 0 * * *',
// watch (boolean) enable watch & restart feature, if a file change in the
// folder or subfolder, your app will get reloaded.
watch: false,
// list of regex to ignore some file or folder names by the watch feature
ignore_watch: ['[/\\]./', 'node_modules'],
// your app will be restarted if it exceeds the amount of memory specified.
// human-friendly format : it can be “10M”, “100K”, “2G” and so on…
max_memory_restart: '1G',
// exec_mode (string) mode to start your app, can be "cluster" or "fork".
// default is "fork".
exec_mode: 'fork',
// (string) error file path (default to $HOME/.pm2/logs/XXXerr.log)
// error_file: '',
// output file path (default to $HOME/.pm2/logs/XXXout.log)
out_file: '/dev/null',
// pid file path (default to $HOME/.pm2/pid/app-pm_id.pid)
// pid_file: '',
env: {
NODE_ENV: 'development',
},
env_production: {
NODE_ENV: 'production',
},
},
],
};