In production Heroku uses Procfile. pm2 roughly emulates this.
Trace npm start
-> node pm2/start.js
Basically pm2/start.js is just a node script that uses pm2's node api to start processes according to the dynamically imported config.
start-logs.js
It starts a child process that runs pm2 logs
, parses, formats and colorizes its output.
http://pm2.keymetrics.io/docs/usage/log-management/
watch-and-restart.js
It sets up filesystem watching and triggers process restarts.
- watch
- can be true, false, or an array of file paths
- if false, disables filesystem watching and process restarting
- if true, watches filesystem for changes and restarts process
- if an array is provided, recursively watches provided paths for file changes and restarts process
- ignore_watch
- array of relative paths, strings, or regexes to ignore from file changes observed by watch
const prefix = process.argv[2] || 'start'
const config = require(`./configs/${prefix}.config.js`)
start.config.js
pm2/configs/defaults.js
http://pm2.keymetrics.io/docs/usage/application-declaration/
npm run start:config my.custom
What npm run start:*
configs are available and how they differ from npm start
When you should make changes to default configs
You should change default config if:
You make a code change and the process running that code doesn't restart
and conversely
You make a code change and a process not running that code restarts
Code that is not running should not restart processes, i.e. scripts, migrations, etc.
Examples:
- https://github.com/BuildingConnected/BC/pull/5197/files
- https://github.com/BuildingConnected/BC/pull/5207/files
You can use npx
to run pm2 cli commands:
npx pm2 ls
show all processesnpx pm2 logs worker
show output from only worker processnpx pm2 monit
cool pm2 dashboard
http://pm2.keymetrics.io/docs/usage/quick-start/
Example:
web.profile