pm2学习

Posted on By yuanfang

pm2

官网英文 https://pm2.io/doc/en/runtime/overview/
学习参考 pm2中文文档 学习参考 PM2实用入门指南

入门

// 安装
npm install pm2 -g

// start and add a process to your list
pm2 start app.js
pm2 start app.js --name="name"
pm2 start app.js --watch [--ignore-watch]

// show your list
pm2 ls

// stop and delete a process from the list
pm2 delete app

// stop the process (kill the process but keep it in the process list)
pm2 stop app

// start the process
pm2 start app

// both stop and start
pm2 restart app
pm2 restart app --name="new-name"

// local monitoring 
pm2 monit

// Generate an ecosystem.config.js template with: pm2 init
pm2 init
// This will generate:
module.exports = {
    apps : [{
        name: "app",
        script: "./app.js",
        env: {
            NODE_ENV: "development",
        },
        env_production: {
            NODE_ENV: "production",
        }
    }]
}
// To start this app in a particular environment, use the --env flag:
pm2 start ecosystem.config.js // uses variables from `env`
pm2 start ecosystem.config.js --env production // uses variables from `env_production`

// Load-Balancing (cluster mode) 负载均衡(集群模式)
pm2 start app.js -i max
// Or via your ecosystem file (ecosystem.config.js):
module.exports = {
    apps: [{
        script: "app.js",
        instances: "max",
    }]
}

// watch && restart
pm2 start app.js --watch
// Or via your ecosystem file (ecosystem.config.js):
module.exports = {
    apps: [{
        script: "app.js",
        watch: ["server", "client"],
        // Delay between restart
        watch_delay: 1000,
        ignore_watch : ["node_modules", "client/img"],
        watch_options: {
            "followSymlinks": false
        }
    }]
}

//---log all logs are saved into $HOME/.pm2/logs
// all apps logs
pm2 logs
// only app logs
pm2 logs app
// empty all application logs
pm2 flush
// via your ecosystem file
module.exports = {
    apps: [{
        name: 'app',
        script: 'app.js',
        output: './out.log',
        error: './error.log',
	    log: './combined.outerr.log', //log combines output and error, disabled by default
    }]
}
// split logs into multiple files instead of a big one
pm2 install pm2-logrotate

// 开机自启动
pm2 startup