This guide will help you install and setup the pm2 NodejJS process monitor PM2 from Keymetrics.io for free and manage your node apps performance and exceptions.
Advertisement:
What is PM2?
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks. This is the steps I used on Ubuntu 16.04. This is NOT a paid endorsement (just self-documenting).
Key Features of PM2
PM2 offers web-based monitoring dashboard, exception reporting, load balancer, CPU and memory monitoring, transaction tracer and much more for NodeJS apps.
What is PM2?
Official page: http://pm2.keymetrics.io/
More info https://www.npmjs.com/package/pm2
Install PM2
1 | npm install pm2 -g |
Install Output
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | npm install pm2 -g /usr/bin/pm2 -> /usr/lib/node_modules/pm2/bin/pm2 /usr/bin/pm2-dev -> /usr/lib/node_modules/pm2/bin/pm2-dev /usr/bin/pm2-docker -> /usr/lib/node_modules/pm2/bin/pm2-docker /usr/bin/pm2-runtime -> /usr/lib/node_modules/pm2/bin/pm2-runtime /usr/lib └─┬ pm2@2.10.2 ├─┬ async@2.6.0 │ └── lodash@4.17.5 ├── blessed@0.1.81 ├─┬ chalk@1.1.3 │ ├── ansi-styles@2.2.1 │ ├── escape-string-regexp@1.0.5 │ ├─┬ has-ansi@2.0.0 │ │ └── ansi-regex@2.1.1 │ ├── strip-ansi@3.0.1 │ └── supports-color@2.0.0 ├─┬ chokidar@2.0.3 │ ├─┬ anymatch@2.0.0 │ │ └─┬ micromatch@3.1.10 │ │ ├── arr-diff@4.0.0 │ │ ├─┬ define-property@2.0.2 │ │ │ └─┬ is-descriptor@1.0.2 │ │ │ ├── is-accessor-descriptor@1.0.0 │ │ │ └── is-data-descriptor@1.0.0 │ │ ├─┬ extend-shallow@3.0.2 │ │ │ ├── assign-symbols@1.0.0 │ │ │ └─┬ is-extendable@1.0.1 │ │ │ └── is-plain-object@2.0.4 │ │ ├─┬ extglob@2.0.4 │ │ │ ├─┬ define-property@1.0.0 │ │ │ │ └─┬ is-descriptor@1.0.2 │ │ │ │ ├── is-accessor-descriptor@1.0.0 │ │ │ │ └── is-data-descriptor@1.0.0 │ │ │ ├─┬ expand-brackets@2.1.4 │ │ │ │ ├── debug@2.6.9 │ │ │ │ ├── define-property@0.2.5 │ │ │ │ ├── extend-shallow@2.0.1 │ │ │ │ └── posix-character-classes@0.1.1 │ │ │ └── extend-shallow@2.0.1 │ │ ├── fragment-cache@0.2.1 │ │ ├── kind-of@6.0.2 │ │ ├─┬ nanomatch@1.2.9 │ │ │ ├─┬ is-odd@2.0.0 │ │ │ │ └── is-number@4.0.0 │ │ │ └── is-windows@1.0.2 │ │ ├── object.pick@1.3.0 │ │ └── regex-not@1.0.2 │ ├── async-each@1.0.1 │ ├─┬ braces@2.3.2 │ │ ├── arr-flatten@1.1.0 │ │ ├── array-unique@0.3.2 │ │ ├─┬ extend-shallow@2.0.1 │ │ │ └── is-extendable@0.1.1 │ │ ├─┬ fill-range@4.0.0 │ │ │ ├── extend-shallow@2.0.1 │ │ │ ├─┬ is-number@3.0.0 │ │ │ │ └─┬ kind-of@3.2.2 │ │ │ │ └── is-buffer@1.1.6 │ │ │ ├── repeat-string@1.6.1 │ │ │ └── to-regex-range@2.1.1 │ │ ├── isobject@3.0.1 │ │ ├── repeat-element@1.1.2 │ │ ├─┬ snapdragon@0.8.2 │ │ │ ├─┬ base@0.11.2 │ │ │ │ ├─┬ cache-base@1.0.1 │ │ │ │ │ ├─┬ collection-visit@1.0.0 │ │ │ │ │ │ ├── map-visit@1.0.0 │ │ │ │ │ │ └── object-visit@1.0.1 │ │ │ │ │ ├── get-value@2.0.6 │ │ │ │ │ ├─┬ has-value@1.0.0 │ │ │ │ │ │ └─┬ has-values@1.0.0 │ │ │ │ │ │ └── kind-of@4.0.0 │ │ │ │ │ ├─┬ set-value@2.0.0 │ │ │ │ │ │ └── extend-shallow@2.0.1 │ │ │ │ │ ├─┬ to-object-path@0.3.0 │ │ │ │ │ │ └── kind-of@3.2.2 │ │ │ │ │ ├─┬ union-value@1.0.0 │ │ │ │ │ │ └─┬ set-value@0.4.3 │ │ │ │ │ │ └── extend-shallow@2.0.1 │ │ │ │ │ └─┬ unset-value@1.0.0 │ │ │ │ │ └─┬ has-value@0.3.1 │ │ │ │ │ ├── has-values@0.1.4 │ │ │ │ │ └── isobject@2.1.0 │ │ │ │ ├─┬ class-utils@0.3.6 │ │ │ │ │ ├── arr-union@3.1.0 │ │ │ │ │ ├── define-property@0.2.5 │ │ │ │ │ └─┬ static-extend@0.1.2 │ │ │ │ │ ├── define-property@0.2.5 │ │ │ │ │ └─┬ object-copy@0.1.0 │ │ │ │ │ ├── copy-descriptor@0.1.1 │ │ │ │ │ ├── define-property@0.2.5 │ │ │ │ │ └── kind-of@3.2.2 │ │ │ │ ├── component-emitter@1.2.1 │ │ │ │ ├─┬ define-property@1.0.0 │ │ │ │ │ └─┬ is-descriptor@1.0.2 │ │ │ │ │ ├── is-accessor-descriptor@1.0.0 │ │ │ │ │ └── is-data-descriptor@1.0.0 │ │ │ │ ├─┬ mixin-deep@1.3.1 │ │ │ │ │ ├── for-in@1.0.2 │ │ │ │ │ └── is-extendable@1.0.1 │ │ │ │ └── pascalcase@0.1.1 │ │ │ ├── debug@2.6.9 │ │ │ ├─┬ define-property@0.2.5 │ │ │ │ └─┬ is-descriptor@0.1.6 │ │ │ │ ├─┬ is-accessor-descriptor@0.1.6 │ │ │ │ │ └── kind-of@3.2.2 │ │ │ │ ├─┬ is-data-descriptor@0.1.4 │ │ │ │ │ └── kind-of@3.2.2 │ │ │ │ └── kind-of@5.1.0 │ │ │ ├── extend-shallow@2.0.1 │ │ │ ├── map-cache@0.2.2 │ │ │ ├── source-map@0.5.7 │ │ │ ├─┬ source-map-resolve@0.5.1 │ │ │ │ ├── atob@2.1.0 │ │ │ │ ├── decode-uri-component@0.2.0 │ │ │ │ ├── resolve-url@0.2.1 │ │ │ │ ├── source-map-url@0.4.0 │ │ │ │ └── urix@0.1.0 │ │ │ └── use@3.1.0 │ │ ├─┬ snapdragon-node@2.1.1 │ │ │ ├─┬ define-property@1.0.0 │ │ │ │ └─┬ is-descriptor@1.0.2 │ │ │ │ ├── is-accessor-descriptor@1.0.0 │ │ │ │ └── is-data-descriptor@1.0.0 │ │ │ └─┬ snapdragon-util@3.0.1 │ │ │ └── kind-of@3.2.2 │ │ ├── split-string@3.1.0 │ │ └─┬ to-regex@3.0.2 │ │ └─┬ safe-regex@1.1.0 │ │ └── ret@0.1.15 │ ├─┬ glob-parent@3.1.0 │ │ ├── is-glob@3.1.0 │ │ └── path-dirname@1.0.2 │ ├── inherits@2.0.3 │ ├─┬ is-binary-path@1.0.1 │ │ └── binary-extensions@1.11.0 │ ├─┬ is-glob@4.0.0 │ │ └── is-extglob@2.1.1 │ ├─┬ normalize-path@2.1.1 │ │ └── remove-trailing-separator@1.1.0 │ ├── path-is-absolute@1.0.1 │ ├─┬ readdirp@2.1.0 │ │ ├── graceful-fs@4.1.11 │ │ ├─┬ minimatch@3.0.4 │ │ │ └─┬ brace-expansion@1.1.11 │ │ │ ├── balanced-match@1.0.0 │ │ │ └── concat-map@0.0.1 │ │ ├─┬ readable-stream@2.3.6 │ │ │ ├── core-util-is@1.0.2 │ │ │ ├── isarray@1.0.0 │ │ │ ├── process-nextick-args@2.0.0 │ │ │ ├── safe-buffer@5.1.1 │ │ │ ├── string_decoder@1.1.1 │ │ │ └── util-deprecate@1.0.2 │ │ └── set-immediate-shim@1.0.1 │ └── upath@1.0.4 ├── cli-table-redemption@1.0.1 ├── commander@2.13.0 ├─┬ cron@1.3.0 │ └── moment-timezone@0.5.14 ├─┬ debug@3.1.0 │ └── ms@2.0.0 ├── eventemitter2@1.0.5 ├── fclone@1.0.11 ├── gkt@1.0.0 ├─┬ mkdirp@0.5.1 │ └── minimist@0.0.8 ├── moment@2.22.0 ├─┬ needle@2.2.0 │ ├── debug@2.6.9 │ ├─┬ iconv-lite@0.4.21 │ │ └── safer-buffer@2.1.2 │ └── sax@1.2.4 ├─┬ nssocket@0.6.0 │ ├── eventemitter2@0.4.14 │ └── lazy@1.0.11 ├── pidusage@1.2.0 ├─┬ pm2-axon@3.1.0 │ ├── amp@0.3.1 │ ├── amp-message@0.1.2 │ └── escape-regexp@0.0.1 ├── pm2-axon-rpc@0.5.0 ├─┬ pm2-deploy@0.3.9 │ ├── async@1.5.2 │ └── tv4@1.3.0 ├─┬ pm2-multimeter@0.1.2 │ └── charm@0.1.2 ├─┬ pmx@1.6.4 │ ├── deep-metrics@0.0.1 │ ├── json-stringify-safe@5.0.1 │ └─┬ vxx@1.2.2 │ ├─┬ continuation-local-storage@3.2.1 │ │ ├── async-listener@0.6.9 │ │ └── emitter-listener@1.1.1 │ ├── debug@2.6.9 │ ├── extend@3.0.1 │ ├── is@3.2.1 │ ├── lodash.findindex@4.6.0 │ ├── lodash.isequal@4.5.0 │ ├── lodash.merge@4.6.1 │ ├── methods@1.1.2 │ ├── shimmer@1.2.0 │ └── uuid@3.2.1 ├─┬ promptly@2.2.0 │ └─┬ read@1.0.7 │ └── mute-stream@0.0.7 ├── semver@5.5.0 ├─┬ shelljs@0.7.8 │ ├─┬ glob@7.1.2 │ │ ├── fs.realpath@1.0.0 │ │ ├─┬ inflight@1.0.6 │ │ │ └── wrappy@1.0.2 │ │ └── once@1.4.0 │ ├── interpret@1.1.0 │ └─┬ rechoir@0.6.2 │ └─┬ resolve@1.7.0 │ └── path-parse@1.0.5 ├─┬ source-map-support@0.5.4 │ └── source-map@0.6.1 ├── sprintf-js@1.1.1 ├── v8-compile-cache@1.1.2 ├─┬ vizion@0.2.13 │ └── async@1.5.2 └─┬ yamljs@0.3.0 └─┬ argparse@1.0.10 └── sprintf-js@1.0.3 |
PM2 Pricing
PM2 appears to be for high-end apps but I am only using the free version or PM2 (thanks KeyMetrics)
Create a bucket for your node app
Login to keymetrics.io,
Click Generate New Bucket
Give the bucket a name etc.
You can now link your bucket with your local pm2 installation (keep the keys private (this one no longer exists))
Linking your local pm2 installation with your keymetrics bucket
1 2 3 | pm2 link l3brztzboz25him i6kofelsyfo7xrd [KM] Connecting [Monitoring Enabled] Dashboard access: https://app.keymetrics.io/#/r/i6kofelsyfo7xrd |
To add an existing node app to PM2 type the following.
1 2 | cd /your-node-application-path/ pm2 start yourapp.js -i 0 --name "myappname" |
You can view node apps that pm2 is managing by typing
1 | pm2 status |
I had a two CPU VM and I found that the app I added was added to each of the two CPU (I only needed one) so I needed to delete the second app on my second core
1 | pm2 delete 1 |
Restart the API
1 | pm2 restart myappname |
You can add a single node apps one 1, 3 or max available CPU’s
1 2 3 4 5 6 7 8 | # Start the maximum processes depending on available CPUs pm2 start app.js -i 0 # Start the maximum processes -1 depending on available CPUs pm2 start app.js -i -1 # Start 3 processes pm2 start app.js -i 3 |
Again, to add an existing node app to PM2 type the following.
1 2 | cd /your-node-application-path/ pm2 start yourapp.js -i 0 --name "myappname" |
Now you can view node app data online. If you don’t have a node app ready you can use the test app.
You can monitor your node app locally too from the CLI.
You can also view a demo bucket at keymetrix.io
PM2’s one age documentation can be found here.
I hope this guide helps someone.
Ask a question or recommend an article
Revision History
v1.0 Initial post