PM2 is a daemon process manager that will help you manage and keep your application online 24/7
# 命令行启动程序
pm2 start .\flask-app.py --name flask-app --log .\log\flask-app.log --time --watch
–name application name
–log output log path
–time log with time
–watch watch and restart app when files change
flask-app.py
1 2 3 4 5 6 7 8 9 from flask import Flaskapp = Flask(__name__) @app.route("/" ) def hello (): return "Hello World !" app.run('127.0.0.1' , 5000 , debug=False )
# 根据配置文件启动程序
1 pm2 start/restart/reload/delete ecosystem.config.js --env production --only koa-app
cosystem.config.js launch by config file
–env assign environment
–only only launch assigned app
ecosystem.config.js
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 module .exports = { apps : [{ name : 'koa-app' , script : 'koa-app.js' , error_file : './log/koa-app-err.log' , out_file : './log/koa-app.log' , merge_logs : true , args : 'one two' , instances : 1 , autorestart : true , watch : true , max_memory_restart : '1G' , env : { NODE_ENV : 'development' }, env_production : { NODE_ENV : 'production' }, ignore_watch : ['./node_modules' , './log' ] }], };
koa-app.js
1 2 3 4 5 6 7 8 9 10 const Koa = require ('koa' )const app = new Koa ()console .log ('Koa2 App' )app.use (async (ctx, next) => { ctx.body = 'Hello Koa2' }) app.listen (3000 )
# 后台启动监听程序
pm2 start .\watch.js --log log\watch.log
watch.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 const chokidar = require ('chokidar' );const watcher = chokidar.watch ('./input' , { ignored : /(^|[\/\\])\../ , persistent : true }); const log = console .log .bind (console );watcher .on ('add' , path => log (`File ${path} has been added` )) .on ('change' , path => log (`File ${path} has been changed` )) .on ('unlink' , path => log (`File ${path} has been removed` ));