====== PM2 ====== Production process manager for Node.js apps with a built-in load balancer. http://pm2.io/ ===== Usage ===== Start 2 instances of application in cluster mode: pm2 start -i 2 --name [name] bin/www Set pm2 start on boot: pm2 startup Save pm2 current process list: pm2 save Remove init script: pm2 unstartup [systemv|systemd] Manually Restore Processes: pm2 resurrect ===== gracefulReload ===== ==== stop ==== 拦截 **SIGINT** 信号,并关闭所有资源,然后退出。 process.on('SIGINT', function () { process.exit(0) }) ==== start ==== 配置 PM2 启动参数,添加 ''--wait-ready'' ,然后在应用成功监听端口,添加 process.send('ready') ==== 信号处理流程 ==== 当一个进程被停止或者重启时,发送信号的顺序如下: - **SIGINT** 信号发送至进程,可在此拦截并退出进程; - 如果 [[http://pm2.keymetrics.io/docs/usage/signals-clean-restart/#customize-exit-delay|1.6s]] 后进程还未退出,则发送 **SIGKILL** 信号强制其退出。 ===== Configuration ===== A PM2 configuration sample: { "name": "<%= appname %>", "script": "./server/index.js", "instances": 2, "exec_mode" : "cluster", "wait-ready": true, "cwd": "." } Run ''pm2 start pm2.json''